Class StringUtil
java.lang.Object
com.aquima.interactions.foundation.text.StringUtil
Utility class providing various string operations.
- Since:
- 5.0
- Author:
- O. Kerpershoek
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Comparator
<String> Comparator that compares string in a non-case-sensitive manner. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Checks whetherhaystack
containsneedle
.static int
createHashCode
(String string) Generates a hashCode for a string which is equal in Java as both in .NET C#.static boolean
This method compares strings case sensitive to determine if the strings are equal.static String
insertParameters
(String intermediate, String[] parameters) This method replaces tokens in the intermediate string with the parameters passed as second argument.static boolean
isDigit
(char c) This method returns true if the character is a number in the [0-9] range.static boolean
Check if string is null or is an empty String.static String
Joins the specified string array into a single string, separating each element with the specified delimiter.static String
static String
joinIgnoreNull
(String[] array, String delimiter) Joins the specified string array into a single string, separating each element with the specified delimiter.static String
Returns a trimmed and toLowerCase String WhenisEmpty(String)
returns true: null will be returned.static boolean
This method tests if the specified string is not empty.static String
Returns the input string or the empty string if the input string is null.static String
replaceAll
(String input, String pattern, String replacement) Replaces all occurrences of a pattern in an input string with a provided replacement string.static String
replaceAll
(String input, Pattern pattern, String replacement) static String
replaceInString
(String original, String token, String newToken) This method will replace all occurrences of the first token with the new token in the original string.static String[]
The array returned by this method contains each substring of this string that is terminated by another substring or the end of the string.static String[]
splitOnRegex
(String str, String pattern) Same assplit
, except that this method takes a regular expression as separatorstatic String
This method returns a pretty printed array.static String
This method provides a null safe trim operation.static String
valueOf
(boolean b) Returns the string representation of the boolean argument.static String
valueOf
(char c) Returns the string representation of the char argument.static String
valueOf
(char[] data) Returns the string representation of the char array argument.static String
valueOf
(double d) Returns the string representation of the double argument.static String
valueOf
(float f) Returns the string representation of the float argument.static String
valueOf
(int i) Returns the string representation of the int argument.static String
valueOf
(long l) Returns the string representation of the long argument.static String
Returns the string representation of the Object argument.
-
Field Details
-
CASE_INSENSITIVE_ORDER
Comparator that compares string in a non-case-sensitive manner. This variable can be used instead of String.CASE_INSENSITIVE_ORDER, as .NET does not have such a default comparator.
-
-
Method Details
-
normalize
Returns a trimmed and toLowerCase String WhenisEmpty(String)
returns true: null will be returned. This method may be used to generate case insensitive key strings for maps.- Parameters:
str
- The string that should be normalized.- Returns:
- The normalized variant of the provided string.
-
split
The array returned by this method contains each substring of this string that is terminated by another substring or the end of the string. The substrings in the array are in the order in which they occur in this string. If the substring is not found in the input then the resulting array has just one element, namely this string. Empty entries including a trailing empty entry will be included in the result.- Parameters:
str
- The string that should be split into multiple strings.separator
- The separator that should be used to split the string on.- Returns:
- Array containing the strings (without the separator) that resulted from the split operation.
-
splitOnRegex
Same assplit
, except that this method takes a regular expression as separator- Parameters:
str
- The string that should be split into multiple stringspattern
- the regular expression pattern to split the string on- Returns:
- Array containing the strings that were split by the pattern. If the pattern was not matched, the array contains the original string. Note that trailing empty strings are discarded (limit = 0)
-
join
-
join
Joins the specified string array into a single string, separating each element with the specified delimiter. If the array is null, this method returns null in order to distinguish between a joined empty array and a joined null array. If the delimiter is null, the array elements will be concatenated without a delimiter.- Parameters:
array
- the array to join (may be null)delimiter
- the separator between elements (may be null)- Returns:
- the concatenation of the array elements, separated by delimiter
- Since:
- 9.7.1
-
joinIgnoreNull
Joins the specified string array into a single string, separating each element with the specified delimiter. If the array is null, this method returns null in order to distinguish between a joined empty array and a joined null array. If the delimiter is null, the array elements will be concatenated without a delimiter.- Parameters:
array
- the array to join (may be null)delimiter
- the separator between elements (may be null)- Returns:
- the concatenation of the array elements, separated by delimiter
- Since:
- 12.4
-
replaceAll
Replaces all occurrences of a pattern in an input string with a provided replacement string.- Parameters:
input
- the input stringpattern
- the regular expression that is searched for in the input stringreplacement
- the string which is replaced at every occurrence of the pattern in the input string Note that the replacement is first processed byMatcher.quoteReplacement(String)
to turn it into a literal replacement string, so the \ and $ sign have no special meaning, as opposed to the default behavior ofString.replaceAll(String, String)
- Returns:
- a string with all occurrences of pattern in the input string replaced with the replacement string. If the pattern is not found, the input string is returned.
-
replaceAll
-
contains
Checks whetherhaystack
containsneedle
. If either haystack or needle are null or empty, this method returnsfalse
.- Parameters:
haystack
- the string to search inneedle
- the string to search for- Returns:
true
if haystack contains needle,false
otherwise.
-
trim
This method provides a null safe trim operation. When the passed argument was null, a null value will be returned, where as a non-null string will be trimmed of excess spaces.- Parameters:
str
- The string that should be trimmed of excess (leading and trailing) spaces.- Returns:
- The string without spaces at the begin or end, or null when a null value was provided.
-
equal
This method compares strings case sensitive to determine if the strings are equal. If both strings are null or empty, the strings are also considered equal. Please note that leading/trailing spaces are ignored during comparison.- Parameters:
first
- The first string that should be compared.second
- The second string that should be compared.- Returns:
- boolean indicating if the first and second string are equal.
-
isEmpty
Check if string is null or is an empty String. A string is considered empty when it only consists of whitespace characters (including newline and tab characters).- Parameters:
str
- The string that should be checked.- Returns:
- Boolean indicating if the string was null or empty.
-
notEmpty
This method tests if the specified string is not empty. A string is considered empty when it only consists of whitespace characters (including newline and tab characters).- Parameters:
str
- The string that should be checked.- Returns:
- Boolean indicating if the string is not empty (contains at least one non-whitespace character)
-
replaceInString
This method will replace all occurrences of the first token with the new token in the original string.- Parameters:
original
- The original string containing the tokens that should be replaced.token
- The token that should be replaced.newToken
- The replacement of the token.- Returns:
- String with all the tokens replaced by new tokens.
-
valueOf
Returns the string representation of the boolean argument.- Parameters:
b
- boolean- Returns:
- if the argument is true, a string equal to "true" is returned; otherwise, a string equal to "false" is returned.
-
valueOf
Returns the string representation of the char argument.- Parameters:
c
- char- Returns:
- the string representation of the char argument.
-
valueOf
Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.- Parameters:
data
- a char array- Returns:
- a newly allocated string representing the same sequence of characters contained in the character array argument
-
valueOf
Returns the string representation of the double argument.- Parameters:
d
- a double.- Returns:
- a string representation of the double argument.
-
valueOf
Returns the string representation of the float argument.- Parameters:
f
- a float.- Returns:
- a string representation of the float argument.
-
valueOf
Returns the string representation of the int argument.- Parameters:
i
- an int.- Returns:
- a string representation of the int argument.
-
valueOf
Returns the string representation of the long argument.- Parameters:
l
- a long.- Returns:
- a string representation of the long argument.
-
valueOf
Returns the string representation of the Object argument.- Parameters:
obj
- an Object.- Returns:
- if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
-
insertParameters
This method replaces tokens in the intermediate string with the parameters passed as second argument. A text containing:'abc {0}{1}{2}' with new String[] { "a", "b", "c", "d" } => 'abc abc' 'abc {0}{1}{2}' with new String[] { "a", "b", "c" } => 'abc abc' 'abc {0}{1}{2}' with new String[] { "a", "b" } => 'abc ab{2}'
- Parameters:
intermediate
- the input string which contains the parameter definitionsparameters
- the parameters- Returns:
- String containing the parameter values at the appropriate places.
-
createHashCode
Generates a hashCode for a string which is equal in Java as both in .NET C#.- Parameters:
string
- the string to hash.- Returns:
- the hashcode, 0 when the string is null
-
toString
This method returns a pretty printed array. The toString method of all objects will be used to construct the pretty printed array. For example: String[] { "first", "second" } will return "[first, second]"- Parameters:
array
- The array to pretty print (null will return "null")- Returns:
- The pretty printed array.
-
isDigit
public static boolean isDigit(char c) This method returns true if the character is a number in the [0-9] range.- Parameters:
c
- The character to test.- Returns:
- A boolean indicating if the character is a number in the [0-9] range.
-
notNull
Returns the input string or the empty string if the input string is null.- Parameters:
value
- the value- Returns:
- the input string or the empty string if the input string is null.
-