Class StringUtilities
Key Features
- String Comparison:
- Case-sensitive and case-insensitive equality
- Comparison with automatic trimming
- Null-safe operations
- CharSequence support
- Content Analysis:
- Empty and whitespace checking
- String length calculations
- Character/substring counting
- Pattern matching with wildcards
- String Manipulation:
- Advanced trimming operations
- Quote handling
- Encoding conversions
- Random string generation
- Distance Metrics:
- Levenshtein distance calculation
- Damerau-Levenshtein distance calculation
Security Configuration
StringUtilities provides configurable security controls to prevent various attack vectors. All security features are disabled by default for backward compatibility.
Security controls can be enabled via system properties:
stringutilities.security.enabled=false— Master switch for all security featuresstringutilities.max.hex.decode.size=0— Max hex string size for decode() (0=disabled)stringutilities.max.wildcard.length=0— Max wildcard pattern length (0=disabled)stringutilities.max.wildcard.count=0— Max wildcard characters in pattern (0=disabled)stringutilities.max.levenshtein.string.length=0— Max string length for Levenshtein distance (0=disabled)stringutilities.max.damerau.levenshtein.string.length=0— Max string length for Damerau-Levenshtein distance (0=disabled)stringutilities.max.repeat.count=0— Max repeat count for repeat() method (0=disabled)stringutilities.max.repeat.total.size=0— Max total size for repeat() result (0=disabled)
Security Features
- Memory Exhaustion Protection: Limits string sizes to prevent out-of-memory attacks
- ReDoS Prevention: Limits wildcard pattern complexity to prevent regular expression denial of service
- Integer Overflow Protection: Prevents arithmetic overflow in size calculations
Usage Examples
String Comparison:
// Case-sensitive and insensitive comparison
boolean equals = StringUtilities.equals("text", "text"); // true
boolean equals = StringUtilities.equalsIgnoreCase("Text", "text"); // true
// Comparison with trimming
boolean equals = StringUtilities.equalsWithTrim(" text ", "text"); // true
Content Checking:
// Empty and whitespace checking
boolean empty = StringUtilities.isEmpty(" "); // true
boolean empty = StringUtilities.isEmpty(null); // true
boolean hasContent = StringUtilities.hasContent(" text "); // true
// Length calculations
int len = StringUtilities.length(null); // 0
int len = StringUtilities.trimLength(" text "); // 4
String Manipulation:
// Trimming operations
String result = StringUtilities.trimToEmpty(null); // ""
String result = StringUtilities.trimToNull(" "); // null
String result = StringUtilities.trimEmptyToDefault(" ", "default"); // "default"
// Quote handling
String result = StringUtilities.removeLeadingAndTrailingQuotes("\"text\""); // text
// Set conversion
Set<String> set = StringUtilities.commaSeparatedStringToSet("a,b,c"); // [a, b, c]
Distance Calculations:
// Edit distance metrics
int distance = StringUtilities.levenshteinDistance("kitten", "sitting"); // 3
int distance = StringUtilities.damerauLevenshteinDistance("book", "back"); // 2
Thread Safety
All methods in this class are stateless and thread-safe.
- Author:
- Ken Partlow, John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcamelToSnake(String camel) Convert acamelCaseorPascalCasestring tosnake_case.commaSeparatedStringToSet(String commaSeparatedString) Converts a comma-separated string into aSetof trimmed, non-empty strings.static booleancontainsIgnoreCase(String s1, String s2) Checks if the first string contains the second string, ignoring case considerations.static intcount(CharSequence content, CharSequence token) Count the number of times that 'token' occurs within 'content'.static intstatic StringcreateString(byte[] bytes, String encoding) Convert a byte[] into a String with a particular encoding.static StringcreateUTF8String(byte[] bytes) Convert a byte[] into a UTF-8 encoded String.static intdamerauLevenshteinDistance(CharSequence source, CharSequence target) Calculate the Damerau-Levenshtein Distance between two strings.static byte[]Convert a hexadecimalStringinto a byte array.static Stringencode(byte[] bytes) Convert a byte array into a printable format containing a String of hex digit characters (two per byte).static booleanequals(CharSequence cs1, CharSequence cs2) Compares two CharSequences, returningtrueif they represent equal sequences of characters.static booleanstatic booleanequalsIgnoreCase(CharSequence cs1, CharSequence cs2) Compares two CharSequences, returningtrueif they represent equal sequences of characters, ignoring case.static booleanequalsIgnoreCase(String s1, String s2) static booleanequalsIgnoreCaseWithTrim(String s1, String s2) static booleanequalsWithTrim(String s1, String s2) static byte[]Convert a String into a byte[] with a particular encoding.static chargetRandomChar(Random random, boolean upper) static StringgetRandomString(Random random, int minLen, int maxLen) Generate a random proper‑case string.static byte[]Convert a String into a byte[] encoded by UTF-8.static booleanhasContent(String s) Checks if a String is not empty (""), not null and not whitespace only.static intComputes a case-insensitive hash code for a CharSequence.static intGet the hashCode of a String, insensitive to case, without any new Strings being created on the heap.static booleanisEmpty(CharSequence cs) Checks if a CharSequence is empty (""), null, or only whitespace.static booleanstatic booleanDetermine if the supplied string contains only numeric digits.static booleanChecks if a CharSequence is empty (""), null or whitespace only.static intlastIndexOf(String path, char ch) static intlength(CharSequence cs) Gets a CharSequence length or0if the CharSequence isnull.static intstatic intThe Levenshtein distance is a string metric for measuring the difference between two sequences.static StringPad the supplied string on the left with spaces until it reaches the specified length.static StringPad the supplied string on the right with spaces until it reaches the specified length.static StringRemoves all leading and trailing double quotes from a String.static StringRepeat a stringcounttimes.static StringReverse the characters of a string.static StringsnakeToCamel(String snake) Convert asnake_casestring tocamelCase.static StringRemoves control characters (char <= 32) from both ends of this String, handlingnullby returningnull.static StringtrimEmptyToDefault(String value, String defaultValue) Trims a string, If the string trims to empty then we return the default.static inttrimLength(String s) Returns the length of the trimmed string.static StringtrimToEmpty(String value) Trims a string, its null safe and null will return empty string here..static StringtrimToNull(String value) Trims a string, If the string trims to empty then we return null.static StringwildcardToRegexString(String wildcard) Convert strings containing DOS-style '*' or '?'
-
Field Details
-
FOLDER_SEPARATOR
-
EMPTY
- See Also:
-
-
Method Details
-
equals
Compares two CharSequences, returningtrueif they represent equal sequences of characters.nulls are handled without exceptions. Twonullreferences are considered to be equal. The comparison is case-sensitive.- Parameters:
cs1- the first CharSequence, may benullcs2- the second CharSequence, may benull- Returns:
trueif the CharSequences are equal (case-sensitive), or bothnull- See Also:
-
equals
- See Also:
-
equalsIgnoreCase
Compares two CharSequences, returningtrueif they represent equal sequences of characters, ignoring case.nulls are handled without exceptions. Twonullreferences are considered equal. The comparison is case insensitive.- Parameters:
cs1- the first CharSequence, may benullcs2- the second CharSequence, may benull- Returns:
trueif the CharSequences are equal (case-insensitive), or bothnull- See Also:
-
equalsIgnoreCase
-
containsIgnoreCase
Checks if the first string contains the second string, ignoring case considerations.This method uses
String.regionMatches(boolean, int, String, int, int)for optimal performance, avoiding the creation of temporary lowercase strings that would be required withs1.toLowerCase().contains(s2.toLowerCase()).- Parameters:
s1- the string to search within, may benulls2- the substring to search for, may benull- Returns:
trueif s1 contains s2 (case-insensitive),falseotherwise. Returnsfalseif either parameter isnull.
-
equalsWithTrim
-
equalsIgnoreCaseWithTrim
-
isEmpty
Checks if a CharSequence is empty (""), null, or only whitespace.- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is empty or null
-
isEmpty
- See Also:
-
isWhitespace
Checks if a CharSequence is empty (""), null or whitespace only.- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is null, empty or whitespace only
-
hasContent
Checks if a String is not empty (""), not null and not whitespace only.- Parameters:
s- the CharSequence to check, may be null- Returns:
trueif the CharSequence is not empty and not null and not whitespace only
-
length
Gets a CharSequence length or0if the CharSequence isnull.- Parameters:
cs- a CharSequence ornull- Returns:
- CharSequence length or
0if the CharSequence isnull.
-
length
- See Also:
-
trimLength
Returns the length of the trimmed string. If the length is null then it returns 0.- Parameters:
s- the string to get the trimmed length of- Returns:
- the length of the trimmed string, or 0 if the input is null
-
lastIndexOf
-
decode
Convert a hexadecimalStringinto a byte array.If the input length is odd or contains non-hex characters the method returns
null.- Parameters:
s- the hexadecimal string to decode, may not benull- Returns:
- the decoded bytes or
nullif the input is malformed
-
encode
Convert a byte array into a printable format containing a String of hex digit characters (two per byte).- Parameters:
bytes- array representation
-
count
-
count
Count the number of times that 'token' occurs within 'content'.- Returns:
- int count (0 if it never occurs, null is the source string, or null is the token).
-
wildcardToRegexString
Convert strings containing DOS-style '*' or '?' to a regex String. -
levenshteinDistance
The Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one word into the other. The phrase 'edit distance' is often used to refer specifically to Levenshtein distance.- Parameters:
s- String onet- String two- Returns:
- the 'edit distance' (Levenshtein distance) between the two strings.
-
damerauLevenshteinDistance
Calculate the Damerau-Levenshtein Distance between two strings. The basic difference between this algorithm and the general Levenshtein algorithm is that damerau-Levenshtein counts a swap of two characters next to each other as 1 instead of 2. This breaks the 'triangular equality', which makes it unusable for Metric trees. See Wikipedia pages on both Levenshtein and Damerau-Levenshtein and then make your decision as to which algorithm is appropriate for your situation.- Parameters:
source- Source input stringtarget- Target input string- Returns:
- The number of substitutions it would take to make the source string identical to the target string
-
getRandomString
Generate a random proper‑case string.- Parameters:
random- Random instance, must not benullminLen- minimum number of characters (inclusive)maxLen- maximum number of characters (inclusive)- Returns:
- alphabetic string with the first character uppercase
- Throws:
NullPointerException- ifrandomisnullIllegalArgumentException- if length parameters are invalid
-
getRandomChar
-
getBytes
Convert a String into a byte[] with a particular encoding. Preferable used when the encoding is one of the guaranteed Java types and you don't want to have to catch the UnsupportedEncodingException required by Java- Parameters:
s- string to encode into bytesencoding- encoding to use
-
createUTF8String
Convert a byte[] into a UTF-8 encoded String.- Parameters:
bytes- bytes to encode into a string
-
getUTF8Bytes
Convert a String into a byte[] encoded by UTF-8.- Parameters:
s- string to encode into bytes
-
createString
Convert a byte[] into a String with a particular encoding. Preferable used when the encoding is one of the guaranteed Java types and you don't want to have to catch the UnsupportedEncodingException required by Java- Parameters:
bytes- bytes to encode into a stringencoding- encoding to use
-
hashCodeIgnoreCase
Computes a case-insensitive hash code for a CharSequence. This method produces the same hash as cs.toString().toLowerCase().hashCode() for compatibility with existing code.- Parameters:
cs- the CharSequence to hash (can be String, StringBuilder, etc.)- Returns:
- the case-insensitive hash code, or 0 if cs is null
-
hashCodeIgnoreCase
Get the hashCode of a String, insensitive to case, without any new Strings being created on the heap.This implementation uses a fast ASCII shift approach for compatible locales, and falls back to the more correct but slower Locale-aware approach for locales where simple ASCII case conversion does not work properly.
-
trim
Removes control characters (char <= 32) from both ends of this String, handlingnullby returningnull.The String is trimmed using
String.trim(). Trim removes start and end characters <= 32.- Parameters:
str- the String to be trimmed, may be null- Returns:
- the trimmed string,
nullif null String input
-
trimToEmpty
Trims a string, its null safe and null will return empty string here..- Parameters:
value- string input- Returns:
- String trimmed string, if value was null this will be empty
-
trimToNull
Trims a string, If the string trims to empty then we return null.- Parameters:
value- string input- Returns:
- String, trimmed from value. If the value was empty we return null.
-
trimEmptyToDefault
Trims a string, If the string trims to empty then we return the default.- Parameters:
value- string inputdefaultValue- value to return on empty or null- Returns:
- trimmed string, or defaultValue when null or empty
-
removeLeadingAndTrailingQuotes
Removes all leading and trailing double quotes from a String. Multiple consecutive quotes at the beginning or end of the string will all be removed.Examples:
- "text" → text
- ""text"" → text
- """text""" → text
- "text with "quotes" inside" → text with "quotes" inside
- Parameters:
input- the String from which to remove quotes (may be null)- Returns:
- the String with all leading and trailing quotes removed, or null if input was null
-
commaSeparatedStringToSet
Converts a comma-separated string into aSetof trimmed, non-empty strings.This method splits the provided string by commas, trims whitespace from each resulting substring, filters out any empty strings, and collects the unique strings into a
Set. If the input string isnullor empty after trimming, the method returns an empty set.Usage Example:
String csv = "apple, banana, cherry, apple, "; Set<String> fruitSet = commaSeparatedStringToSet(csv); // fruitSet contains ["apple", "banana", "cherry"]Note: The resulting
Setdoes not maintain the insertion order. If order preservation is required, consider using aLinkedHashSet.- Parameters:
commaSeparatedString- the comma-separated string to convert- Returns:
- a mutable
Setcontaining the trimmed, unique, non-empty substrings from the input string. Returns an emptyLinkedHashSetif the input isnull, empty, or contains only whitespace. - Throws:
IllegalArgumentException- if the method is modified to disallownullinputs in the future- See Also:
-
snakeToCamel
Convert asnake_casestring tocamelCase.- Parameters:
snake- the snake case string, may benull- Returns:
- the camelCase representation or
nullifsnakeisnull
-
camelToSnake
Convert acamelCaseorPascalCasestring tosnake_case.- Parameters:
camel- the camel case string, may benull- Returns:
- the snake_case representation or
nullifcamelisnull
-
isNumeric
Determine if the supplied string contains only numeric digits.- Parameters:
s- the string to test, may benull- Returns:
trueifsis non-empty and consists solely of digits
-
repeat
Repeat a stringcounttimes.- Parameters:
s- the string to repeat, may benullcount- the number of times to repeat, must be non-negative- Returns:
- the repeated string or
nullifsisnull - Throws:
IllegalArgumentException- ifcountis negative
-
reverse
Reverse the characters of a string.- Parameters:
s- the string to reverse, may benull- Returns:
- the reversed string or
nullifsisnull
-
padLeft
Pad the supplied string on the left with spaces until it reaches the specified length. If the string is already longer thanlength, the original string is returned.- Parameters:
s- the string to pad, may benulllength- desired final length- Returns:
- the padded string or
nullifsisnull
-
padRight
Pad the supplied string on the right with spaces until it reaches the specified length. If the string is already longer thanlength, the original string is returned.- Parameters:
s- the string to pad, may benulllength- desired final length- Returns:
- the padded string or
nullifsisnull
-