Class StringUtils
- java.lang.Object
-
- com.inductiveautomation.snap.util.StringUtils
-
public class StringUtils extends java.lang.Object
This class is a collection of convenient static String utils.
-
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
add(java.lang.String s1, java.lang.String s2)
Adds the two strings together (treats nulls as empty strings).static java.lang.String
add(java.lang.String s1, java.lang.String s2, java.lang.String aSeparator)
Adds the two strings together with separator (treats nulls as empty strings, omitting sep if either is null).static int
addWords(java.lang.String aString, int aStart, int anEnd, java.lang.StringBuffer aBuffer, java.util.regex.Matcher aMatcher)
Adds words from given string and string start/end to given string buffer.static boolean
containsIC(java.lang.CharSequence aString1, java.lang.CharSequence aString2)
Returns whether a given string contains a given string, ignoring case.static boolean
containsIC(java.lang.String[] theStrings, java.lang.String aString)
Returns whether the given string array contains the given string, ignoring case.static java.lang.String
delete(java.lang.String aString, java.lang.String aSearch)
Returns the result of deleting from the given string any occurrence of the search string.static java.lang.String
deleteIC(java.lang.String aString, java.lang.String aSearch)
Returns the result of deleting from the given string any occurrence of the search string (ignores case).static double
doubleValue(java.lang.String aString)
Returns an double value by parsing the given string.static double
doubleValue(java.lang.String aString, int aStart)
Returns an double value by parsing the given string starting at the given index.static boolean
endsWithIC(java.lang.String s1, java.lang.String s2)
Returns whether s1 ends with s2, ignoring case.static boolean
endsWithIC(java.lang.String s1, java.lang.String... strings)
Returns whether s1 ends with any of the given strings, ignoring case.static boolean
equalsIC(java.lang.String s1, java.lang.String s2)
Returns whether s1 equals s2, ignoring case.static boolean
equalsIC(java.lang.String s1, java.lang.String... strings)
Returns whether s1 equals any of the given strings, ignoring case.static java.lang.String
firstCharLowerCase(java.lang.String aString)
Returns the given string with the first char demoted to lowercase.static java.lang.String
firstCharUpperCase(java.lang.String aString)
Returns the given string with the first char promoted to uppercase.static float
floatValue(java.lang.String aString)
Returns an float value by parsing the given string.static java.lang.String
fromCamelCase(java.lang.String aString)
Returns a spaced string from a camel case string.static byte[]
getBytes(java.lang.String aString)
Returns the ASCII bytes of the given string (ISO-Latin).static byte[]
getBytes(java.lang.String aString, java.lang.String enc)
Returns the bytes of the given string in the requested char encoding.static java.lang.String
getISOLatinString(byte[] bytes)
Returns a string from the given ASCII bytes.static java.lang.String
getISOLatinString(byte[] bytes, int offset, int length)
Returns a string from the given ASCII bytes (from offset to offset+length).static java.lang.String
getPathChild(java.lang.String aPath, java.lang.String aChildPath)
Returns a path with a filename or relative path added.static java.lang.String
getPathExtension(java.lang.String aPath)
Returns the extension of the given string path (everything after last '.').static java.lang.String
getPathFileName(java.lang.String aPath)
Returns the file name component of the given string path (everything after last file separator).static int
getPathFileNameIndex(java.lang.String aPath)
Returns the index to the first character of the filename component of the given path.static java.lang.String
getPathFileNameSimple(java.lang.String aPath)
Returns the simple file name for a given path (just the file name without extension).static java.lang.String
getPathParent(java.lang.String aPath)
Returns the given string path minus the file name component (everything after last file separator).static java.lang.String
getPathPeer(java.lang.String aPath, java.lang.String aName)
Returns a peer path for given path and new filename by stripping filename from given path and using given name.static java.lang.String
getPathSimple(java.lang.String aPath)
Returns the given string path minus any extension.static java.lang.String
getPathSister(java.lang.String aPath, java.lang.String anExtension)
Returns a sister path for given path and new extension by stripping extension from path and using given extension.static java.lang.String
getPathStandardized(java.lang.String aPath)
Returns the path in a standard, normal format (strips any trailing file separators).static java.lang.String
getStackTraceString(java.lang.Throwable aThrowable)
Returns a stack trace string for given exception.static java.lang.String
getString(byte[] theBytes)
Returns a string for given bytes.static java.lang.String
getString(byte[] theBytes, java.lang.String anEncoding)
Returns a string for given bytes.static java.lang.String
getStringQuoted(java.lang.String aString)
Returns a quoted string.static java.lang.String
getStringSingleQuoted(java.lang.String aString)
Returns a single quoted string.static java.lang.String
getStringSurrounded(java.lang.String aString, java.lang.String aSurroundString, java.lang.String anEscapeString)
Returns a string surrounded by given string.static int
indexOfIC(java.lang.CharSequence s1, java.lang.CharSequence s2)
Returns the index of search string s2 in given string s1, ignores case.static int
indexOfIC(java.lang.CharSequence s1, java.lang.CharSequence s2, int start)
Returns the index of search string s2 in given string s1, ignores case and starts at start char index.static int
indexOfIC(java.lang.String[] theStrings, java.lang.String aString)
Returns the index of a given string in the given array, ignoring case.static int
intValue(java.lang.String aString)
Returns an int value by parsing the given string.static boolean
isEmpty(java.lang.String aString)
Returns whether string is null or empty.static java.lang.String
join(java.lang.Object[] parts, java.lang.String separator)
Allows you to take an array of strings and concatenate them together with a separatorstatic java.lang.String
join(java.lang.Object[] parts, java.lang.String separator, int aStart, int anEnd)
Allows you to take an array of strings and concatenate them together with a separatorstatic int
length(java.lang.CharSequence aString)
Returns the length of given string (supports null).static long
longValue(java.lang.String aString)
Returns an int value by parsing the given string.static long
longValue(java.lang.String aString, int aStart)
Returns an double value by parsing the given string starting at the given index.static java.lang.String
max(java.lang.String s)
String promotion - returns either the given string or empty string (if given string is null).static java.lang.String
min(java.lang.String s)
String demotion - returns either the given string or null (if given string length is zero).static java.lang.String
nextInSequence(java.lang.String source, java.lang.String suffix)
Turns a string like "myFile" into "myFile-1", and a string like "myFile-2" to "myFile-3", given suffix like "-".static java.lang.String
or(java.lang.String s1, java.lang.String s2)
Returns the first non-null string of the two given strings (or null).static java.lang.String
replace(java.lang.String s, int start, int end, java.lang.String withString)
Returns the result of replacing in the given string the char range with the with-string.static java.lang.String
replace(java.lang.String aString, java.lang.String search, char replace)
Returns the result of replacing in the given string any occurrence of the search string with the replace-char.static java.lang.String
replace(java.lang.String aString, java.lang.String search, java.lang.String replace)
Returns the result of replacing in the given string any occurrence of the search string with the replace-string.static java.lang.String
replaceIC(java.lang.String aString, java.lang.String search, java.lang.String replace)
Returns the result of replacing in given string any occurrence of search string with replace-string (ignore case).static java.util.List<java.lang.String>
separate(java.lang.String aString, java.lang.String aSeparator)
Returns a list of parts of given string separated by the given delimiter.static java.util.List<java.lang.String>
separate(java.lang.String aString, java.lang.String aSeparator, boolean doTrim)
Returns a list of parts of given string separated by the given delimiter, with option to trim space.static boolean
startsWithIC(java.lang.String s1, java.lang.String s2)
Returns whether s1 starts with s2, ignoring case.static boolean
startsWithIC(java.lang.String s1, java.lang.String... strings)
Returns whether s1 starts with any of the given strings, ignoring case.static java.lang.String
toCamelCase(java.lang.String aString)
Returns a camel cased string from a string with non-alphanumeric chars.static java.lang.String
toString(double aValue)
Returns a string representation of the given float to (at most) 3 significant digits.static java.lang.String
trimEnd(java.lang.String aString)
Trims the end of a string.static java.lang.String
wrap(java.lang.String aString, int aLimit)
Returns a string wrapped on word boundaries.static java.lang.String
wrap(java.lang.String aString, int aLimit, java.lang.String aSeperator, java.lang.String aJoiner)
Returns a string wrapped on word boundaries as defined by regex string (eg., whitespace="\\s*", dot="\\.", etc.).
-
-
-
Method Detail
-
isEmpty
public static boolean isEmpty(java.lang.String aString)
Returns whether string is null or empty.
-
length
public static int length(java.lang.CharSequence aString)
Returns the length of given string (supports null).
-
add
public static java.lang.String add(java.lang.String s1, java.lang.String s2)
Adds the two strings together (treats nulls as empty strings).
-
add
public static java.lang.String add(java.lang.String s1, java.lang.String s2, java.lang.String aSeparator)
Adds the two strings together with separator (treats nulls as empty strings, omitting sep if either is null).
-
min
public static java.lang.String min(java.lang.String s)
String demotion - returns either the given string or null (if given string length is zero).
-
max
public static java.lang.String max(java.lang.String s)
String promotion - returns either the given string or empty string (if given string is null).
-
or
public static java.lang.String or(java.lang.String s1, java.lang.String s2)
Returns the first non-null string of the two given strings (or null).
-
toString
public static java.lang.String toString(double aValue)
Returns a string representation of the given float to (at most) 3 significant digits.
-
getPathFileName
public static java.lang.String getPathFileName(java.lang.String aPath)
Returns the file name component of the given string path (everything after last file separator).
-
getPathFileNameSimple
public static java.lang.String getPathFileNameSimple(java.lang.String aPath)
Returns the simple file name for a given path (just the file name without extension).
-
getPathFileNameIndex
public static int getPathFileNameIndex(java.lang.String aPath)
Returns the index to the first character of the filename component of the given path.
-
getPathExtension
public static java.lang.String getPathExtension(java.lang.String aPath)
Returns the extension of the given string path (everything after last '.').
-
getPathSimple
public static java.lang.String getPathSimple(java.lang.String aPath)
Returns the given string path minus any extension.
-
getPathParent
public static java.lang.String getPathParent(java.lang.String aPath)
Returns the given string path minus the file name component (everything after last file separator).
-
getPathChild
public static java.lang.String getPathChild(java.lang.String aPath, java.lang.String aChildPath)
Returns a path with a filename or relative path added.
-
getPathPeer
public static java.lang.String getPathPeer(java.lang.String aPath, java.lang.String aName)
Returns a peer path for given path and new filename by stripping filename from given path and using given name.
-
getPathSister
public static java.lang.String getPathSister(java.lang.String aPath, java.lang.String anExtension)
Returns a sister path for given path and new extension by stripping extension from path and using given extension.
-
getPathStandardized
public static java.lang.String getPathStandardized(java.lang.String aPath)
Returns the path in a standard, normal format (strips any trailing file separators).
-
trimEnd
public static java.lang.String trimEnd(java.lang.String aString)
Trims the end of a string.
-
delete
public static java.lang.String delete(java.lang.String aString, java.lang.String aSearch)
Returns the result of deleting from the given string any occurrence of the search string.
-
deleteIC
public static java.lang.String deleteIC(java.lang.String aString, java.lang.String aSearch)
Returns the result of deleting from the given string any occurrence of the search string (ignores case).
-
replace
public static java.lang.String replace(java.lang.String s, int start, int end, java.lang.String withString)
Returns the result of replacing in the given string the char range with the with-string.
-
replace
public static java.lang.String replace(java.lang.String aString, java.lang.String search, char replace)
Returns the result of replacing in the given string any occurrence of the search string with the replace-char.
-
replace
public static java.lang.String replace(java.lang.String aString, java.lang.String search, java.lang.String replace)
Returns the result of replacing in the given string any occurrence of the search string with the replace-string.
-
replaceIC
public static java.lang.String replaceIC(java.lang.String aString, java.lang.String search, java.lang.String replace)
Returns the result of replacing in given string any occurrence of search string with replace-string (ignore case).
-
separate
public static java.util.List<java.lang.String> separate(java.lang.String aString, java.lang.String aSeparator)
Returns a list of parts of given string separated by the given delimiter.
-
separate
public static java.util.List<java.lang.String> separate(java.lang.String aString, java.lang.String aSeparator, boolean doTrim)
Returns a list of parts of given string separated by the given delimiter, with option to trim space.
-
intValue
public static int intValue(java.lang.String aString)
Returns an int value by parsing the given string.
-
longValue
public static long longValue(java.lang.String aString)
Returns an int value by parsing the given string.
-
longValue
public static long longValue(java.lang.String aString, int aStart)
Returns an double value by parsing the given string starting at the given index.
-
floatValue
public static float floatValue(java.lang.String aString)
Returns an float value by parsing the given string.
-
doubleValue
public static double doubleValue(java.lang.String aString)
Returns an double value by parsing the given string.
-
doubleValue
public static double doubleValue(java.lang.String aString, int aStart)
Returns an double value by parsing the given string starting at the given index.
-
getBytes
public static byte[] getBytes(java.lang.String aString)
Returns the ASCII bytes of the given string (ISO-Latin).
-
getBytes
public static byte[] getBytes(java.lang.String aString, java.lang.String enc)
Returns the bytes of the given string in the requested char encoding.
-
getString
public static java.lang.String getString(byte[] theBytes)
Returns a string for given bytes.
-
getString
public static java.lang.String getString(byte[] theBytes, java.lang.String anEncoding)
Returns a string for given bytes.
-
getISOLatinString
public static java.lang.String getISOLatinString(byte[] bytes)
Returns a string from the given ASCII bytes.
-
getISOLatinString
public static java.lang.String getISOLatinString(byte[] bytes, int offset, int length)
Returns a string from the given ASCII bytes (from offset to offset+length).
-
indexOfIC
public static int indexOfIC(java.lang.CharSequence s1, java.lang.CharSequence s2)
Returns the index of search string s2 in given string s1, ignores case.
-
indexOfIC
public static int indexOfIC(java.lang.CharSequence s1, java.lang.CharSequence s2, int start)
Returns the index of search string s2 in given string s1, ignores case and starts at start char index.
-
containsIC
public static boolean containsIC(java.lang.CharSequence aString1, java.lang.CharSequence aString2)
Returns whether a given string contains a given string, ignoring case.
-
equalsIC
public static boolean equalsIC(java.lang.String s1, java.lang.String s2)
Returns whether s1 equals s2, ignoring case.
-
equalsIC
public static boolean equalsIC(java.lang.String s1, java.lang.String... strings)
Returns whether s1 equals any of the given strings, ignoring case.
-
endsWithIC
public static boolean endsWithIC(java.lang.String s1, java.lang.String s2)
Returns whether s1 ends with s2, ignoring case.
-
endsWithIC
public static boolean endsWithIC(java.lang.String s1, java.lang.String... strings)
Returns whether s1 ends with any of the given strings, ignoring case.
-
startsWithIC
public static boolean startsWithIC(java.lang.String s1, java.lang.String s2)
Returns whether s1 starts with s2, ignoring case.
-
startsWithIC
public static boolean startsWithIC(java.lang.String s1, java.lang.String... strings)
Returns whether s1 starts with any of the given strings, ignoring case.
-
firstCharUpperCase
public static java.lang.String firstCharUpperCase(java.lang.String aString)
Returns the given string with the first char promoted to uppercase.
-
firstCharLowerCase
public static java.lang.String firstCharLowerCase(java.lang.String aString)
Returns the given string with the first char demoted to lowercase.
-
fromCamelCase
public static java.lang.String fromCamelCase(java.lang.String aString)
Returns a spaced string from a camel case string.
-
toCamelCase
public static java.lang.String toCamelCase(java.lang.String aString)
Returns a camel cased string from a string with non-alphanumeric chars.
-
wrap
public static java.lang.String wrap(java.lang.String aString, int aLimit)
Returns a string wrapped on word boundaries.
-
wrap
public static java.lang.String wrap(java.lang.String aString, int aLimit, java.lang.String aSeperator, java.lang.String aJoiner)
Returns a string wrapped on word boundaries as defined by regex string (eg., whitespace="\\s*", dot="\\.", etc.).
-
addWords
public static int addWords(java.lang.String aString, int aStart, int anEnd, java.lang.StringBuffer aBuffer, java.util.regex.Matcher aMatcher)
Adds words from given string and string start/end to given string buffer.
-
join
public static java.lang.String join(java.lang.Object[] parts, java.lang.String separator)
Allows you to take an array of strings and concatenate them together with a separator- Parameters:
parts
- an array of Stringsseparator
- a string to place between elements- Returns:
-
join
public static java.lang.String join(java.lang.Object[] parts, java.lang.String separator, int aStart, int anEnd)
Allows you to take an array of strings and concatenate them together with a separator- Parameters:
parts
- an array of Stringsseparator
- a string to place between elements- Returns:
-
nextInSequence
public static java.lang.String nextInSequence(java.lang.String source, java.lang.String suffix)
Turns a string like "myFile" into "myFile-1", and a string like "myFile-2" to "myFile-3", given suffix like "-".
-
indexOfIC
public static int indexOfIC(java.lang.String[] theStrings, java.lang.String aString)
Returns the index of a given string in the given array, ignoring case.
-
containsIC
public static boolean containsIC(java.lang.String[] theStrings, java.lang.String aString)
Returns whether the given string array contains the given string, ignoring case.
-
getStringQuoted
public static java.lang.String getStringQuoted(java.lang.String aString)
Returns a quoted string.
-
getStringSingleQuoted
public static java.lang.String getStringSingleQuoted(java.lang.String aString)
Returns a single quoted string.
-
getStringSurrounded
public static java.lang.String getStringSurrounded(java.lang.String aString, java.lang.String aSurroundString, java.lang.String anEscapeString)
Returns a string surrounded by given string.
-
getStackTraceString
public static java.lang.String getStackTraceString(java.lang.Throwable aThrowable)
Returns a stack trace string for given exception.
-
-