Class StringUtil
These string utilities provide both convenience methods and performance improvements over most standard library versions. The main aim of the optimizations is to avoid object creation unless absolutely required.
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
append
(StringBuilder buf, byte b, int base) append hex digitstatic void
append
(StringBuilder buf, String s, int offset, int length) Append substring to StringBuilderstatic void
append2digits
(StringBuilder buf, int i) Append 2 digits (zero padded) to the StringBuilderstatic String[]
Parse the string representation of a list usingcsvSplit(List, String, int, int)
static boolean
asciiEndsWithIgnoreCase
(String string, String suffix) Check for a string suffix, ignoringStandardCharsets.US_ASCII
case differences.static boolean
asciiEqualsIgnoreCase
(String string, String other) Check for string equality, ignoringStandardCharsets.US_ASCII
case differences.static boolean
asciiStartsWithIgnoreCase
(String string, String prefix) Check for a string prefix, ignoringStandardCharsets.US_ASCII
case differences.static byte
asciiToLowerCase
(byte c) fast lower case conversion.static char
asciiToLowerCase
(char c) fast lower case conversion.static String
fast lower case conversion.static String
fast upper case conversion.static String[]
Parse a CSV string usingcsvSplit(List, String, int, int)
static String[]
Parse a CSV string usingcsvSplit(List, String, int, int)
Split a quoted comma separated string to a liststatic boolean
endsWithIgnoreCase
(String string, String suffix) Deprecated.static boolean
static byte[]
static byte[]
static byte[]
static int
Get the length of a string where a null string is length 0.static byte[]
static int
returns the next index of a character from the chars stringstatic int
Find the index of a control characters in Stringstatic boolean
Test if a string is null or only has whitespace characters in it.static boolean
Checks if a String is empty ("") or null.static boolean
static boolean
isNotBlank
(String str) Test if a string is not null and contains at least 1 non-whitespace characters in it.static String
Return a non null string.static String
printable
(byte[] b) static String
static String
randomAlphaNumeric
(int digits) static String
Replace chars within string.static String
Replace substrings within string.static String
replaceFirst
(String original, String target, String replacement) Replace first substrings within string.static String
Replace all characters from input string that are known to have special meaning in various filesystems.static String
sanitizeXmlString
(String html) static boolean
startsWithIgnoreCase
(String string, String prefix) Deprecated.static String
static String
toHexString
(byte b) static String
toHexString
(byte[] b) static String
toHexString
(byte[] b, int offset, int length) static int
Convert String to an integer.toListNonNull
(String... strings) Convert an array of strings to a list of non-null strings.static String
Deprecated, for removal: This API element is subject to removal in a future version.useString(byte[], int, int, Charset)
insteadstatic String
toUTF8String
(byte[] b, int offset, int length) static String
Truncate a string to a max size.static String
The String value of an Object
-
Field Details
-
CRLF
- See Also:
-
DEFAULT_DELIMS
- See Also:
-
-
Method Details
-
asciiToLowerCase
public static char asciiToLowerCase(char c) fast lower case conversion. Only works on ascii (not unicode)- Parameters:
c
- the char to convert- Returns:
- a lower case version of c
-
asciiToLowerCase
public static byte asciiToLowerCase(byte c) fast lower case conversion. Only works on ascii (not unicode)- Parameters:
c
- the byte to convert- Returns:
- a lower case version of c
-
asciiToLowerCase
fast lower case conversion. Only works on ascii (not unicode)- Parameters:
s
- the string to convert- Returns:
- a lower case version of s
-
asciiToUpperCase
fast upper case conversion. Only works on ascii (not unicode)- Parameters:
s
- the string to convert- Returns:
- a lower case version of s
-
sanitizeFileSystemName
Replace all characters from input string that are known to have special meaning in various filesystems.This will replace all of the following characters with a "
_
" (underscore).- Control Characters
- Anything not 7-bit printable ASCII
- Special characters: pipe, redirect, combine, slash, equivalence, bang, glob, selection, etc...
- Space
- Parameters:
str
- the raw input string- Returns:
- the sanitized output string. or null if
str
is null.
-
asciiEqualsIgnoreCase
Check for string equality, ignoringStandardCharsets.US_ASCII
case differences.- Parameters:
string
- The string to checkother
- The other string to check- Returns:
- true if the strings are equal, ignoring
StandardCharsets.US_ASCII
case differences.
-
startsWithIgnoreCase
Deprecated.Check for a string prefix, ignoringStandardCharsets.US_ASCII
case differences.- Parameters:
string
- The string to checkprefix
- The sub string to look for as a prefix- Returns:
- true if the string ends with the substring, ignoring
StandardCharsets.US_ASCII
case differences.
-
asciiStartsWithIgnoreCase
Check for a string prefix, ignoringStandardCharsets.US_ASCII
case differences.- Parameters:
string
- The string to checkprefix
- The sub string to look for as a prefix- Returns:
- true if the string ends with the substring, ignoring
StandardCharsets.US_ASCII
case differences.
-
endsWithIgnoreCase
Deprecated.Check for a string suffix, ignoringStandardCharsets.US_ASCII
case differences.- Parameters:
string
- The string to checksuffix
- The sub string to look for as a suffix- Returns:
- true if the string ends with the substring, ignoring
StandardCharsets.US_ASCII
case differences.
-
asciiEndsWithIgnoreCase
Check for a string suffix, ignoringStandardCharsets.US_ASCII
case differences.- Parameters:
string
- The string to checksuffix
- The sub string to look for as a suffix- Returns:
- true if the string ends with the substring, ignoring
StandardCharsets.US_ASCII
case differences.
-
indexFrom
returns the next index of a character from the chars string- Parameters:
s
- the input string to searchchars
- the chars to look for- Returns:
- the index of the character in the input stream found.
-
replace
Replace chars within string.Fast replacement for
java.lang.String#
String.replace(char, char)
- Parameters:
str
- the input stringfind
- the char to look forwith
- the char to replace with- Returns:
- the now replaced string
-
replace
Replace substrings within string.Fast replacement for
java.lang.String#
String.replace(CharSequence, CharSequence)
- Parameters:
s
- the input stringsub
- the string to look forwith
- the string to replace with- Returns:
- the now replaced string
-
replaceFirst
Replace first substrings within string.Fast replacement for
java.lang.String#
String.replaceFirst(String, String)
, but without Regex support.- Parameters:
original
- the original stringtarget
- the target string to look forreplacement
- the replacement string to use- Returns:
- the replaced string
-
append
Append substring to StringBuilder- Parameters:
buf
- StringBuilder to append tos
- String to append fromoffset
- The offset of the substringlength
- The length of the substring
-
append
append hex digit- Parameters:
buf
- the buffer to append tob
- the byte to appendbase
- the base of the hex output (almost always 16).
-
append2digits
Append 2 digits (zero padded) to the StringBuilder- Parameters:
buf
- the buffer to append toi
- the value to append
-
nonNull
Return a non null string.- Parameters:
s
- String- Returns:
- The string passed in or empty string if it is null.
-
toListNonNull
Convert an array of strings to a list of non-null strings.- Parameters:
strings
- the array- Returns:
- The list of non-null strings.
- See Also:
-
equals
-
toUTF8String
-
toString
@Deprecated(since="10", forRemoval=true) public static String toString(byte[] b, int offset, int length, String charset) Deprecated, for removal: This API element is subject to removal in a future version.useString(byte[], int, int, Charset)
instead -
indexOfControlChars
Find the index of a control characters in StringThis will return a result on the first occurrence of a control character, regardless if there are more than one.
Note: uses codepoint version of
Character.isISOControl(int)
to support Unicode better.indexOfControlChars(null) == -1 indexOfControlChars("") == -1 indexOfControlChars("\r\n") == 0 indexOfControlChars("\t") == 0 indexOfControlChars(" ") == -1 indexOfControlChars("a") == -1 indexOfControlChars(".") == -1 indexOfControlChars(";\n") == 1 indexOfControlChars("abc\f") == 3 indexOfControlChars("z\010") == 1 indexOfControlChars(":") == 1
- Parameters:
str
- the string to test.- Returns:
- the index of first control character in string, -1 if no control characters encountered
-
isBlank
Test if a string is null or only has whitespace characters in it.Note: uses codepoint version of
Character.isWhitespace(int)
to support Unicode better.isBlank(null) == true isBlank("") == true isBlank("\r\n") == true isBlank("\t") == true isBlank(" ") == true isBlank("a") == false isBlank(".") == false isBlank(";\n") == false
- Parameters:
str
- the string to test.- Returns:
- true if string is null or only whitespace characters, false if non-whitespace characters encountered.
-
isEmpty
Checks if a String is empty ("") or null.
isEmpty(null) == true isEmpty("") == true isEmpty("\r\n") == false isEmpty("\t") == false isEmpty(" ") == false isEmpty("a") == false isEmpty(".") == false isEmpty(";\n") == false
- Parameters:
str
- the string to test.- Returns:
- true if string is null or empty.
-
getLength
Get the length of a string where a null string is length 0.- Parameters:
s
- the string.- Returns:
- the length of the string.
-
isNotBlank
Test if a string is not null and contains at least 1 non-whitespace characters in it.Note: uses codepoint version of
Character.isWhitespace(int)
to support Unicode better.isNotBlank(null) == false isNotBlank("") == false isNotBlank("\r\n") == false isNotBlank("\t") == false isNotBlank(" ") == false isNotBlank("a") == true isNotBlank(".") == true isNotBlank(";\n") == true
- Parameters:
str
- the string to test.- Returns:
- true if string is not null and has at least 1 non-whitespace character, false if null or all-whitespace characters.
-
isHex
-
fromHexString
-
toHexString
-
toHexString
-
toHexString
-
printable
-
printable
-
getBytes
-
getBytes
-
getUtf8Bytes
-
toInt
Convert String to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown- Parameters:
string
- A String containing an integer.from
- The index to start parsing from- Returns:
- an int
-
truncate
Truncate a string to a max size.- Parameters:
str
- the string to possibly truncatemaxSize
- the maximum size of the string- Returns:
- the truncated string. if
str
param is null, then the returned string will also be null.
-
arrayFromString
Parse the string representation of a list usingcsvSplit(List, String, int, int)
- Parameters:
s
- The string to parse, expected to be enclosed as '[...]'- Returns:
- An array of parsed values.
-
csvSplit
Parse a CSV string usingcsvSplit(List, String, int, int)
- Parameters:
s
- The string to parse- Returns:
- An array of parsed values.
-
csvSplit
Parse a CSV string usingcsvSplit(List, String, int, int)
- Parameters:
s
- The string to parseoff
- The offset into the string to start parsinglen
- The len in characters to parse- Returns:
- An array of parsed values.
-
csvSplit
Split a quoted comma separated string to a listHandle rfc4180-like CSV strings, with the exceptions:
- quoted values may contain double quotes escaped with back-slash
- Non-quoted values are trimmed of leading trailing white space
- trailing commas are ignored
- double commas result in a empty string value
- Parameters:
list
- The Collection to split to (or null to get a new list)s
- The string to parseoff
- The offset into the string to start parsinglen
- The len in characters to parse- Returns:
- list containing the parsed list values
-
sanitizeXmlString
-
strip
-
valueOf
The String value of an ObjectThis method calls
String.valueOf(Object)
unless the object is null, in which case null is returned- Parameters:
object
- The object- Returns:
- String value or null
-
randomAlphaNumeric
-
asciiEndsWithIgnoreCase(String, String)