@Exported public class StringTools extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
StringTools.RegexAppendCallback
Allows a user to append/modify a buffer during a call to evaluateRegexWithCallback
|
| Modifier and Type | Field and Description |
|---|---|
static String |
BOOLEAN_FALSE
Boolean string "false"
|
static String |
BOOLEAN_NO
Boolean string "no" - analog for "false"
|
static String |
BOOLEAN_OFF
Boolean string "off" - analog for "false"
|
static String |
BOOLEAN_ON
Boolean string "on" - analog for "true"
|
static String |
BOOLEAN_TRUE
Boolean string "true"
|
static String |
BOOLEAN_YES
Boolean string "yes" - analog for "true"
|
static char |
CHR_BACK_SLASH
Character for "\"
|
static char |
CHR_DOT
Character for "."
|
static char |
CHR_FORWARD_SLASH
Character for "/"
|
static char |
CHR_TAB
Tab character
|
static String |
STR_AND
String constant for "&" sign
|
static String |
STR_COMMA
String constant for "," sign
|
static String |
STR_EQUALS
String constant for "=" sign
|
static String |
STR_QUESTION
String constant for "?" sign
|
static String |
STR_SEMICOLON
String constant for ";" sign
|
static String |
STR_SPACE
String constant for " " sign
|
| Constructor and Description |
|---|
StringTools() |
| Modifier and Type | Method and Description |
|---|---|
static String |
change(String input,
String oldPattern,
String newPattern)
Replaces the specified substring with the new substring in the given input.
|
static String |
concat(String s1,
String s2)
Fast 2 string concatenation using
StringBuffer. |
static String |
evaluateRegexWithCallback(String text,
String regEx,
StringTools.RegexAppendCallback callback)
Analyze a string and invoke a callback when a match is found.
|
static String[] |
getArrayFromCsvString(String csv)
Parses comma (",") delimited string to string array.
|
static String |
getCsvFromCollection(Collection collection)
Constructs string from a collection
|
static List<String> |
getDelimitedList(String value,
String delims)
Get
List of tokens from delimited original string |
static String |
getFirstSegment(String value,
String separator)
Get first string segment separated by passed separator
|
static int[] |
getIntArrayFromDelimitedString(String data,
String delimiter)
Parses string with the specified delimiter and returns array of ints as a result of
parsing.
|
static String |
getLastSegment(String value,
String separator)
Get last string segment separated by passed separator
|
static String |
getLastSegment(String value,
String separator,
boolean includeSeparator)
Get last string segment separated by passed separator
|
static List<String> |
getListFromCsv(String value)
|
static List<String> |
getListFromDelimitedString(String data,
String delimiter)
Parses data with the specified delimiter
|
static List<Long> |
getLongsListFromCsv(String value)
|
static String |
getSegment(String str,
String separator,
int segment)
Returns the segment, null if not found, first segment is segment 0
|
static String |
getValueOrDefault(String value,
String defaultValue)
Checks the value and returns a non-null value or the default value
|
static boolean |
isEmpty(char[] array)
Verifies whether passed character array is empty/null or not.
|
static boolean |
isEmpty(CharSequence sequence)
Verifies whether passed character sequence is empty/null or not.
|
static boolean |
isEmpty(String str)
Verifies whether passed string is empty/null or not.
|
static boolean |
isTrue(String value)
Test if the value of a string is considered true.
|
static String |
replaceChars(String orig,
String chars,
char replace)
Replaces all chars with the replacement char
|
static String |
replaceParams(String source,
String[] params)
Replace placeholders, or parameters, in a string that are of the format {number}
|
static String |
stripFirstSegment(String value,
String separator)
Strips string first segment, defined by separator and returns stripped string
|
static String |
stripLastSegment(String value,
String separator)
Strips string last segment, defined by separator and returns stripped string
|
static boolean |
toBoolean(String value)
Converts string to boolean.
|
static String |
toCsv(List<String> strings)
Convert
List of strings to comma-separated string |
static String |
toCsv(Object[] objects)
Convert array of objects to comma-separated string
|
static String |
unwrap(String value,
String prefix,
String postfix)
Unwraps string using selected prefix and postfix
|
static String |
wrap(String value,
String prefix,
String postfix)
Wrap string value with prefix and postfix.
|
static String |
wrapIfRequired(String value,
String prefix,
String postfix)
Wrap the string value with prefix and postfix, but only if the prefix and/or postfix does not exist in the value.
|
public static final String BOOLEAN_ON
public static final String BOOLEAN_OFF
public static final String BOOLEAN_YES
public static final String BOOLEAN_NO
public static final String BOOLEAN_FALSE
public static final String BOOLEAN_TRUE
public static final String STR_COMMA
public static final String STR_SEMICOLON
public static final String STR_QUESTION
public static final String STR_EQUALS
public static final String STR_AND
public static final String STR_SPACE
public static final char CHR_DOT
public static final char CHR_FORWARD_SLASH
public static final char CHR_BACK_SLASH
public static final char CHR_TAB
public static List<String> getListFromDelimitedString(String data, String delimiter)
data - Original data to parsedelimiter - Delimiter to use for data parsingLinkedList of tokens parsed, delimiters are not returnedpublic static int[] getIntArrayFromDelimitedString(String data, String delimiter)
data - Original data to parsedelimiter - Delimiter to use for data parsingpublic static String[] getArrayFromCsvString(String csv)
csv - Comma separated valuesString extracted from linepublic static String getCsvFromCollection(Collection collection)
collection - source collection of valuespublic static String getFirstSegment(String value, String separator)
value - initial stringseparator - is separator stringpublic static String getLastSegment(String value, String separator)
value - initial stringseparator - is separator stringpublic static String getLastSegment(String value, String separator, boolean includeSeparator)
value - initial stringseparator - is separator stringincludeSeparator - true to include the separator character in the result, false otherwisepublic static String getSegment(String str, String separator, int segment)
str - Source stringseparator - Parsing separatorsegment - String segment numberpublic static String stripFirstSegment(String value, String separator)
value - initial stringseparator - is separator stringpublic static String stripLastSegment(String value, String separator)
value - initial stringseparator - is separator stringpublic static boolean isEmpty(String str)
str - Source string to analyzepublic static boolean isEmpty(CharSequence sequence)
sequence - Source sequence to analyzepublic static boolean isEmpty(char[] array)
array - Source array to analyzepublic static String change(String input, String oldPattern, String newPattern)
input - the string to examineoldPattern - a substring representing a pattern to be replacednewPattern - a substring representing the pattern to replace the oldpublic static String replaceChars(String orig, String chars, char replace)
orig - Original stringchars - Characters we want to replacereplace - Replacement characterpublic static boolean toBoolean(String value)
value - Source string value to convertpublic static boolean isTrue(String value)
value - String to be testedpublic static String wrap(String value, String prefix, String postfix)
result := prefix + value + postfix
value - Source string value to wrapprefix - Prefix to usepostfix - Postfix to usepublic static String wrapIfRequired(String value, String prefix, String postfix)
value - string value to wrapprefix - the string to prependpostfix - the string to appendpublic static String unwrap(String value, String prefix, String postfix)
value - Previously wrapped stringprefix - Prefix to usepostfix - Postfix to usepublic static String toCsv(List<String> strings)
List of strings to comma-separated stringstrings - Original strings containerpublic static String toCsv(Object[] objects)
objects - array of objects (each will converted by calling toString())public static List<String> getDelimitedList(String value, String delims)
List of tokens from delimited original stringvalue - Original string valuedelims - Delimiters used to make tokenspublic static String replaceParams(String source, String[] params)
Example: Calling on string "Error locating resource {0} for user {1}" with array {"root", "admin"}
would result in "Error locating resource root for user admin"
source - a string with parameters, starting with 0params - the values for the parameters, the index corresponding to the parameter value + 1public static String concat(String s1, String s2)
StringBuffer.s1 - source string #1s2 - source string #2public static String evaluateRegexWithCallback(String text, String regEx, StringTools.RegexAppendCallback callback)
text - the text to analyzeregEx - the regular expression to matchcallback - the callback to invoke when a match is found