Class StringUtils
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
abbreviate
(String in, int length) Abbreviates a String using ellipses.static byte[]
base64Decode
(String in) BASE64-decodes the specified string.static String
Shortcut for callingbase64Decode(String) and converting the result to a UTF-8 encoded string.static String
base64Encode
(byte[] in) BASE64-encodes the specified byte array.static String
Shortcut for callingbase64Encode(in.getBytes(
"UTF-8" ))static String
Converts the specified object to a comma-delimited list.static char
Returns the character at the specified index in the string without throwing exceptions.static int
Compares two strings, but gracefully handlesnulls .static byte[]
Converts string into a GZipped input stream.static boolean
contains
(String value, CharSequence substring) Same asString.contains(CharSequence)
except returnsnull if the value is null.static boolean
containsAny
(String s, char... chars) Returnstrue if the specified string contains any of the specified characters.static int
countChars
(String s, char c) Counts the number of the specified character in the specified string.static String
Debug method for rendering non-ASCII character sequences.static String
decompress
(byte[] is) Converts a GZipped input stream into a string.static int
diffPosition
(String s1, String s2) Finds the position where the two strings differ.static int
diffPositionIc
(String s1, String s2) Finds the position where the two strings differ ignoring case.static String
Returns an empty string if the specified object isnull .static String
Returns an empty string if the specified string isnull .static boolean
An efficient method for checking if a string ends with a character.static boolean
Same asendsWith(String, char)
except check for multiple characters.static boolean
Tests two strings for equality, but gracefully handles nulls.static boolean
Tests two strings for equality, but gracefully handles nulls.static boolean
Tests two strings for case-insensitive equality, but gracefully handles nulls.static String
escapeChars
(String s, AsciiSet escaped) Escapes the specified characters in the string.static char
Returns the first character in the specified string.static String
firstNonEmpty
(String... s) Returns the first non-null, non-empty string in the list.static char
Returns the first non-whitespace character in the string.static String
Attempts to escape any invalid characters found in a URI.static String
Similar toMessageFormat.format(String, Object...)
except allows you to specify POJO arguments.static byte[]
Converts a hexadecimal character string to a byte array.static String
fromHexToUTF8
(String hex) Converts a hexadecimal byte stream (e.g.static byte[]
fromSpacedHex
(String hex) Same asfromHex(String)
except expects spaces between the byte strings.static String
Converts a space-deliminted hexadecimal byte stream (e.g.static String
Given an absolute URI, returns just the authority portion (e.g.static long
Parses a duration string.static Pattern
Converts a string containing"*" meta characters with a regular expression pattern.static Pattern
getMatchPattern
(String s, int flags) Converts a string containing"*" meta characters with a regular expression pattern.static String
Takes in a string, splits it by lines, and then prepends each line with line numbers.static String
getNumberedLines
(String s, int start, int end) Same asgetNumberedLines(String)
except only returns the specified lines.static int
Same asString.indexOf(int)
except allows you to check for multiple characters.static boolean
Efficiently determines whether a URL is of the pattern "xxx://xxx"static boolean
Returnstrue if the specified string is numeric.static boolean
Returnstrue if specified charsequence isnull or empty.static boolean
Returnstrue if specified string isnull or empty.static boolean
Returnstrue if specified string isnull or empty or consists of only blanks.static boolean
isFirstNumberChar
(char c) Returnstrue if the specified character is a valid first character for a number.static boolean
Returnstrue if the specified string is a floating point number.static boolean
Returnstrue if the specified string is valid JSON.static boolean
isJsonArray
(Object o, boolean ignoreWhitespaceAndComments) Returnstrue if the specified string appears to be an JSON array.static boolean
isJsonObject
(Object o, boolean ignoreWhitespaceAndComments) Returnstrue if the specified string appears to be a JSON object.static boolean
isNotEmpty
(String s) Returnstrue if specified string is notnull or empty.static boolean
isNotEmpty
(String s1, String s2) Returnstrue if either of the specified strings are notnull or empty.static boolean
isNumberChar
(char c) Returnstrue if the specified character is a valid number character.static boolean
Returnstrue if this string can be parsed byparseNumber(String, Class)
.static boolean
Returnstrue if the specified string is one of the specified values.static boolean
Efficiently determines whether a URL is of the pattern "xxx:/xxx".static String
join
(int[] tokens, char d) Join the specified tokens into a delimited string.static String
Joins the specified tokens into a delimited string.static StringBuilder
join
(Object[] tokens, char d, StringBuilder sb) Join the specified tokens into a delimited string and writes the output to the specified string builder.static String
Join the specified tokens into a delimited string.static String
join
(Collection<?> tokens, char d) Join the specified tokens into a delimited string.static String
join
(Collection<?> tokens, String d) Join the specified tokens into a delimited string.static StringBuilder
join
(Collection<?> tokens, String d, StringBuilder sb) Joins the specified tokens into a delimited string and writes the output to the specified string builder.static String
Join the specified tokens into a delimited string.static String
Join the specified tokens into a delimited string.static StringBuilder
join
(List<?> tokens, String d, StringBuilder sb) Joins the specified tokens into a delimited string and writes the output to the specified string builder.static String
Same asjoin(Collection, char)
but escapes the delimiter if found in the tokens.static String
Joins tokens with newlines.static char
Returns the last non-whitespace character in the string.static boolean
Tests two strings for non-equality, but gracefully handles nulls.static boolean
Tests two strings for non-equality ignoring case, but gracefully handles nulls.static String
Returnsnull if the specified string isnull or empty.static Character
Converts aString to aCharacter static int
Converts a string containing a possible multiplier suffix to an integer.static Calendar
parseIsoCalendar
(String date) Parses an ISO8601 string into a calendar.static Date
parseIsoDate
(String date) Parses an ISO8601 string into a date.static long
Converts a string containing a possible multiplier suffix to a long.static Number
parseNumber
(String s, Class<? extends Number> type) Parses a number from the specified string.static String
random
(int numchars) Generated a random UUID with the specified number of characters.static String
Creates a repeated pattern.static String
Replaces"\\uXXXX" character sequences with their unicode characters.static String
replaceVars
(String s, Map<String, Object> m) Simple utility for replacing variables of the form"{key}" with values in the specified map.static String[]
Shortcut for callingsplit(s,
',' )static String[]
Same assplit(String, char)
except splits all strings in the input and returns a single result.static String[]
Splits a character-delimited string into a string array.static String[]
Same assplit(String, char)
but limits the number of tokens returned.static void
Same assplit(String,char)
but consumes the tokens instead of creating an array.static void
Same assplit(String)
but consumes the tokens instead of creating an array.Splits a list of key-value pairs into an ordered map.static String[]
Splits the method arguments in the signature of a method.static String[]
Splits a space-delimited string with optionally quoted arguments.static String[]
splitQuoted
(String s, boolean keepQuotes) Same assplitQuoted(String)
but allows you to optionally keep the quote characters.static boolean
startsWith
(String s, char c) An efficient method for checking if a string starts with a character.static String
CallsObject.toString()
on the specified object if it's not null.static String
Converts the specified array to a string.static String
Strips the first and last character from a string.static String
Strips invalid characters such as CTRL characters from a string meant to be encoded as an HTTP header value.static String
toHex
(byte b) Converts the specified byte into a 2 hexadecimal characters.static String
toHex
(byte[] bytes) Converts a byte array into a simple hexadecimal character string.static char[]
toHex2
(int num) Converts the specified number into a 2 hexadecimal characters.static char[]
toHex4
(int num) Converts the specified number into a 4 hexadecimal characters.static char[]
toHex8
(long num) Converts the specified number into a 8 hexadecimal characters.static String
Converts the specified object to an ISO8601 date string.static String
Converts the specified object to an ISO8601 date-time string.static String
toReadableBytes
(byte[] b) Converts the specified bytes into a readable string.static String
toSpacedHex
(byte[] bytes) Same astoHex(byte[])
but puts spaces between the byte strings.static URI
Converts the specified object to a URI.static String
Same asString.trim()
but preventsNullPointerExceptions .static String
Trims whitespace characters from the end of the specified string.static String
Trims'/' characters from the beginning of the specified string.static String
Trims'/' characters from both the start and end of the specified string.static String
Trims'/' and space characters from both the start and end of the specified string.static String
Trims whitespace characters from the beginning of the specified string.static String
Trims'/' characters from the end of the specified string.static String
unEscapeChars
(String s, AsciiSet escaped) Removes escape characters from the specified characters.static String
unicodeSequence
(char c) Creates an escaped-unicode sequence (e.g.static String
Decodes aapplication/x-www-form-urlencoded string usingUTF-8 encoding scheme.static String
Encodes aapplication/x-www-form-urlencoded string usingUTF-8 encoding scheme.static String
Same asurlEncode(String)
except only escapes characters that absolutely need to be escaped.static String
Similar toURLEncoder.encode(String, String)
but doesn't encode"/" characters.
-
Field Details
-
NOT_EMPTY
Predicate check to filter out null and empty strings.
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
parseNumber
Parses a number from the specified string.- Parameters:
s
- The string to parse the number from.type
- The number type to created. Can be any of the following:- Integer
- Double
- Float
- Long
- Short
- Byte
- BigInteger
- BigDecimal
null orNumber , uses the best guess.- Returns:
- The parsed number, or
null if the string was null.
-
parseCharacter
Converts aString to aCharacter - Parameters:
o
- The string to convert.- Returns:
- The first character of the string if the string is of length 0, or
null if the string isnull or empty.
-
isNumeric
Returnstrue if this string can be parsed byparseNumber(String, Class)
.- Parameters:
s
- The string to check.- Returns:
true if this string can be parsed without causing an exception.
-
isFirstNumberChar
Returnstrue if the specified character is a valid first character for a number.- Parameters:
c
- The character to test.- Returns:
true if the specified character is a valid first character for a number.
-
isFloat
Returnstrue if the specified string is a floating point number.- Parameters:
s
- The string to check.- Returns:
true if the specified string is a floating point number.
-
isDecimal
Returnstrue if the specified string is numeric.- Parameters:
s
- The string to check.- Returns:
true if the specified string is numeric.
-
join
Join the specified tokens into a delimited string.- Parameters:
tokens
- The tokens to join.separator
- The delimiter.- Returns:
- The delimited string. If
tokens isnull , returnsnull .
-
join
Join the specified tokens into a delimited string.- Parameters:
tokens
- The tokens to join.d
- The delimiter.- Returns:
- The delimited string. If
tokens isnull , returnsnull .
-
join
Join the specified tokens into a delimited string.- Parameters:
tokens
- The tokens to join.d
- The delimiter.- Returns:
- The delimited string. If
tokens isnull , returnsnull .
-
join
Joins the specified tokens into a delimited string and writes the output to the specified string builder.- Parameters:
tokens
- The tokens to join.d
- The delimiter.sb
- The string builder to append the response to.- Returns:
- The same string builder passed in as
sb .
-
join
Joins the specified tokens into a delimited string and writes the output to the specified string builder.- Parameters:
tokens
- The tokens to join.d
- The delimiter.sb
- The string builder to append the response to.- Returns:
- The same string builder passed in as
sb .
-
join
Joins the specified tokens into a delimited string.- Parameters:
tokens
- The tokens to join.d
- The delimiter.- Returns:
- The delimited string. If
tokens isnull , returnsnull .
-
join
Join the specified tokens into a delimited string and writes the output to the specified string builder.- Parameters:
tokens
- The tokens to join.d
- The delimiter.sb
- The string builder to append the response to.- Returns:
- The same string builder passed in as
sb .
-
join
Join the specified tokens into a delimited string.- Parameters:
tokens
- The tokens to join.d
- The delimiter.- Returns:
- The delimited string. If
tokens isnull , returnsnull .
-
join
Join the specified tokens into a delimited string.- Parameters:
tokens
- The tokens to join.d
- The delimiter.- Returns:
- The delimited string. If
tokens isnull , returnsnull .
-
join
Join the specified tokens into a delimited string.- Parameters:
tokens
- The tokens to join.d
- The delimiter.- Returns:
- The delimited string. If
tokens isnull , returnsnull .
-
joine
Same asjoin(Collection, char)
but escapes the delimiter if found in the tokens.- Parameters:
tokens
- The tokens to join.d
- The delimiter.- Returns:
- The delimited string. If
tokens isnull , returnsnull .
-
joinnl
Joins tokens with newlines.- Parameters:
tokens
- The tokens to concatenate.- Returns:
- A string with the specified tokens contatenated with newlines.
-
split
Shortcut for callingsplit(s,
',' )- Parameters:
s
- The string to split. Can benull .- Returns:
- The tokens, or
null if the string was null.
-
split
Same assplit(String)
but consumes the tokens instead of creating an array.- Parameters:
s
- The string to split.consumer
- The consumer of the tokens.
-
split
Splits a character-delimited string into a string array.Does not split on escaped-delimiters (e.g. "\,"); Resulting tokens are trimmed of whitespace.
NOTE: This behavior is different than the Jakarta equivalent. split("a,b,c",',') -> {"a","b","c"} split("a, b ,c ",',') -> {"a","b","c"} split("a,,c",',') -> {"a","","c"} split(",,",',') -> {"","",""} split("",',') -> {} split(null,',') -> null split("a,b\,c,d", ',', false) -> {"a","b\,c","d"} split("a,b\\,c,d", ',', false) -> {"a","b\","c","d"} split("a,b\,c,d", ',', true) -> {"a","b,c","d"}
- Parameters:
s
- The string to split. Can benull .c
- The character to split on.- Returns:
- The tokens, or
null if the string was null.
-
split
Same assplit(String,char)
but consumes the tokens instead of creating an array.- Parameters:
s
- The string to split.c
- The character to split on.consumer
- The consumer of the tokens.
-
split
Same assplit(String, char)
but limits the number of tokens returned.- Parameters:
s
- The string to split. Can benull .c
- The character to split on.limit
- The maximum number of tokens to return.- Returns:
- The tokens, or
null if the string was null.
-
split
Same assplit(String, char)
except splits all strings in the input and returns a single result.- Parameters:
s
- The string to split. Can benull .c
- The character to split on.- Returns:
- The tokens.
-
splitMap
Splits a list of key-value pairs into an ordered map.Example:
String
in ="foo=1;bar=2" ; Mapmap = StringUtils.splitMap (in,';' ,'=' ,true );- Parameters:
s
- The string to split.trim
- Trim strings after parsing.- Returns:
- The parsed map. Never
null .
-
containsAny
Returnstrue if the specified string contains any of the specified characters.- Parameters:
s
- The string to test.chars
- The characters to look for.- Returns:
true if the specified string contains any of the specified characters.false if the string isnull .
-
splitQuoted
Splits a space-delimited string with optionally quoted arguments.Examples:
"foo" =>["foo"] " foo " =>["foo"] "foo bar baz" =>["foo","bar","baz"] "foo 'bar baz'" =>["foo","bar baz"] "foo \"bar baz\"" =>["foo","bar baz"] "foo 'bar\'baz'" =>["foo","bar'baz"]
- Parameters:
s
- The input string.- Returns:
- The results, or
null if the input wasnull .
An empty string results in an empty array.
-
splitQuoted
Same assplitQuoted(String)
but allows you to optionally keep the quote characters.- Parameters:
s
- The input string.keepQuotes
- Iftrue , quote characters are kept on the tokens.- Returns:
- The results, or
null if the input wasnull .
An empty string results in an empty array.
-
isEmpty
Returnstrue if specified string isnull or empty.- Parameters:
s
- The string to check.- Returns:
true if specified string isnull or empty.
-
isEmpty
Returnstrue if specified charsequence isnull or empty.- Parameters:
s
- The string to check.- Returns:
true if specified charsequence isnull or empty.
-
isEmptyOrBlank
Returnstrue if specified string isnull or empty or consists of only blanks.- Parameters:
s
- The string to check.- Returns:
true if specified string isnull or emptyor consists of only blanks.
-
isNotEmpty
Returnstrue if specified string is notnull or empty.- Parameters:
s
- The string to check.- Returns:
true if specified string is notnull or empty.
-
isNotEmpty
Returnstrue if either of the specified strings are notnull or empty.- Parameters:
s1
- The string to check.s2
- The string to check.- Returns:
true if either of the specified strings are notnull or empty.
-
nullIfEmpty
Returnsnull if the specified string isnull or empty.- Parameters:
s
- The string to check.- Returns:
null if the specified string isnull or empty, or the same string if not.
-
emptyIfNull
Returns an empty string if the specified string isnull .- Parameters:
s
- The string to check.- Returns:
- An empty string if the specified string is
null , or the same string otherwise.
-
emptyIfNull
Returns an empty string if the specified object isnull .- Parameters:
o
- The object to check.- Returns:
- An empty string if the specified object is
null , or the object converted to a string usingString.toString()
.
-
unEscapeChars
Removes escape characters from the specified characters.- Parameters:
s
- The string to remove escape characters from.escaped
- The characters escaped.- Returns:
- A new string if characters were removed, or the same string if not or if the input was
null .
-
escapeChars
Escapes the specified characters in the string.- Parameters:
s
- The string with characters to escape.escaped
- The characters to escape.- Returns:
- The string with characters escaped, or the same string if no escapable characters were found.
-
decodeHex
Debug method for rendering non-ASCII character sequences.- Parameters:
s
- The string to decode.- Returns:
- A string with non-ASCII characters converted to
"[hex]" sequences.
-
startsWith
An efficient method for checking if a string starts with a character.- Parameters:
s
- The string to check. Can benull .c
- The character to check for.- Returns:
true if the specified string is notnull and starts with the specified character.
-
endsWith
An efficient method for checking if a string ends with a character.- Parameters:
s
- The string to check. Can benull .c
- The character to check for.- Returns:
true if the specified string is notnull and ends with the specified character.
-
endsWith
Same asendsWith(String, char)
except check for multiple characters.- Parameters:
s
- The string to check. Can benull .c
- The characters to check for.- Returns:
true if the specified string is notnull and ends with the specified character.
-
toHex2
Converts the specified number into a 2 hexadecimal characters.- Parameters:
num
- The number to convert to hex.- Returns:
- A
containing the specified characters.char [2]
-
toHex
Converts the specified byte into a 2 hexadecimal characters.- Parameters:
b
- The number to convert to hex.- Returns:
- A
containing the specified characters.char [2]
-
toReadableBytes
Converts the specified bytes into a readable string.- Parameters:
b
- The number to convert to hex.- Returns:
- A
containing the specified characters.char [2]
-
toHex4
Converts the specified number into a 4 hexadecimal characters.- Parameters:
num
- The number to convert to hex.- Returns:
- A
containing the specified characters.char [4]
-
toHex8
Converts the specified number into a 8 hexadecimal characters.- Parameters:
num
- The number to convert to hex.- Returns:
- A
containing the specified characters.char [8]
-
eq
Tests two strings for equality, but gracefully handles nulls.- Parameters:
s1
- String 1.s2
- String 2.- Returns:
true if the strings are equal.
-
eq
Tests two strings for equality, but gracefully handles nulls.- Parameters:
caseInsensitive
- Use case-insensitive matching.s1
- String 1.s2
- String 2.- Returns:
true if the strings are equal.
-
diffPosition
Finds the position where the two strings differ.- Parameters:
s1
- The first string.s2
- The second string.- Returns:
- The position where the two strings differ, or
-1 if they're equal.
-
diffPositionIc
Finds the position where the two strings differ ignoring case.- Parameters:
s1
- The first string.s2
- The second string.- Returns:
- The position where the two strings differ, or
-1 if they're equal.
-
eqic
Tests two strings for case-insensitive equality, but gracefully handles nulls.- Parameters:
s1
- String 1.s2
- String 2.- Returns:
true if the strings are equal.
-
ne
Tests two strings for non-equality, but gracefully handles nulls.- Parameters:
s1
- String 1.s2
- String 2.- Returns:
true if the strings are not equal.
-
neic
Tests two strings for non-equality ignoring case, but gracefully handles nulls.- Parameters:
s1
- String 1.s2
- String 2.- Returns:
true if the strings are not equal ignoring case.
-
base64EncodeToString
Shortcut for callingbase64Encode(in.getBytes(
"UTF-8" ))- Parameters:
in
- The input string to convert.- Returns:
- The string converted to BASE-64 encoding.
-
base64Encode
BASE64-encodes the specified byte array.- Parameters:
in
- The input byte array to convert.- Returns:
- The byte array converted to a BASE-64 encoded string.
-
base64DecodeToString
Shortcut for callingbase64Decode(String) and converting the result to a UTF-8 encoded string.- Parameters:
in
- The BASE-64 encoded string to decode.- Returns:
- The decoded string.
-
base64Decode
BASE64-decodes the specified string.- Parameters:
in
- The BASE-64 encoded string.- Returns:
- The decoded byte array.
-
random
Generated a random UUID with the specified number of characters.Characters are composed of lower-case ASCII letters and numbers only.
This method conforms to the restrictions for hostnames as specified in RFC 952 Since each character has 36 possible values, the square approximation formula for the number of generated IDs that would produce a 50% chance of collision is:
sqrt(36^N) . Dividing this number by 10 gives you an approximation of the number of generated IDs needed to produce a <1% chance of collision.For example, given 5 characters, the number of generated IDs need to produce a <1% chance of collision would be:
sqrt(36^5)/10=777 - Parameters:
numchars
- The number of characters in the generated UUID.- Returns:
- A new random UUID.
-
trim
Same asString.trim()
but preventsNullPointerExceptions .- Parameters:
s
- The string to trim.- Returns:
- The trimmed string, or
null if the string wasnull .
-
strip
Strips the first and last character from a string.- Parameters:
s
- The string to strip.- Returns:
- The striped string, or the same string if the input was
null or less than length 2.
-
parseIsoDate
Parses an ISO8601 string into a date.Supports any of the following formats:
yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS - Parameters:
date
- The date string.- Returns:
- The parsed date.
- Throws:
IllegalArgumentException
- Value was not a valid date.
-
parseIsoCalendar
Parses an ISO8601 string into a calendar.Supports any of the following formats:
yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddThh, yyyy-MM-ddThh:mm, yyyy-MM-ddThh:mm:ss, yyyy-MM-ddThh:mm:ss.SSS - Parameters:
date
- The date string.- Returns:
- The parsed calendar.
- Throws:
IllegalArgumentException
- Value was not a valid date.
-
toIsoDate
Converts the specified object to an ISO8601 date string.- Parameters:
c
- The object to convert.- Returns:
- The converted object.
-
toIsoDateTime
Converts the specified object to an ISO8601 date-time string.- Parameters:
c
- The object to convert.- Returns:
- The converted object.
-
replaceVars
Simple utility for replacing variables of the form"{key}" with values in the specified map.Nested variables are supported in both the input string and map values.
If the map does not contain the specified value, the variable is not replaced.
null values in the map are treated as blank strings.- Parameters:
s
- The string containing variables to replace.m
- The map containing the variable values.- Returns:
- The new string with variables replaced, or the original string if it didn't have variables in it.
-
replaceUnicodeSequences
Replaces"\\uXXXX" character sequences with their unicode characters.- Parameters:
s
- The string to replace unicode sequences in.- Returns:
- A string with unicode sequences replaced.
-
unicodeSequence
Creates an escaped-unicode sequence (e.g."\\u1234" ) for the specified character.- Parameters:
c
- The character to create a sequence for.- Returns:
- An escaped-unicode sequence.
-
stringify
CallsObject.toString()
on the specified object if it's not null.- Parameters:
o
- The object to convert to a string.- Returns:
- The object converted to a string, or
null if the object was null.
-
stringifyDeep
Converts the specified array to a string.- Parameters:
o
- The array to convert to a string.- Returns:
- The array converted to a string, or
null if the object was null.
-
fromHexToUTF8
Converts a hexadecimal byte stream (e.g. "34A5BC") into a UTF-8 encoded string.- Parameters:
hex
- The hexadecimal string.- Returns:
- The UTF-8 string.
-
fromSpacedHexToUTF8
Converts a space-deliminted hexadecimal byte stream (e.g. "34 A5 BC") into a UTF-8 encoded string.- Parameters:
hex
- The hexadecimal string.- Returns:
- The UTF-8 string.
-
toHex
Converts a byte array into a simple hexadecimal character string.- Parameters:
bytes
- The bytes to convert to hexadecimal.- Returns:
- A new string consisting of hexadecimal characters.
-
toSpacedHex
Same astoHex(byte[])
but puts spaces between the byte strings.- Parameters:
bytes
- The bytes to convert to hexadecimal.- Returns:
- A new string consisting of hexadecimal characters.
-
fromHex
Converts a hexadecimal character string to a byte array.- Parameters:
hex
- The string to convert to a byte array.- Returns:
- A new byte array.
-
fromSpacedHex
Same asfromHex(String)
except expects spaces between the byte strings.- Parameters:
hex
- The string to convert to a byte array.- Returns:
- A new byte array.
-
repeat
Creates a repeated pattern.- Parameters:
count
- The number of times to repeat the pattern.pattern
- The pattern to repeat.- Returns:
- A new string consisting of the repeated pattern.
-
trimStart
Trims whitespace characters from the beginning of the specified string.- Parameters:
s
- The string to trim.- Returns:
- The trimmed string, or
null if the string wasnull .
-
trimEnd
Trims whitespace characters from the end of the specified string.- Parameters:
s
- The string to trim.- Returns:
- The trimmed string, or
null if the string wasnull .
-
isOneOf
Returnstrue if the specified string is one of the specified values.- Parameters:
s
- The string to test. Can benull .values
- The values to test. Can containnull .- Returns:
true if the specified string is one of the specified values.
-
trimSlashes
Trims'/' characters from both the start and end of the specified string.- Parameters:
s
- The string to trim.- Returns:
- A new trimmed string, or the same string if no trimming was necessary.
-
trimSlashesAndSpaces
Trims'/' and space characters from both the start and end of the specified string.- Parameters:
s
- The string to trim.- Returns:
- A new trimmed string, or the same string if no trimming was necessary.
-
trimTrailingSlashes
Trims'/' characters from the end of the specified string.- Parameters:
s
- The string to trim.- Returns:
- A new trimmed string, or the same string if no trimming was necessary.
-
trimLeadingSlashes
Trims'/' characters from the beginning of the specified string.- Parameters:
s
- The string to trim.- Returns:
- A new trimmed string, or the same string if no trimming was necessary.
-
urlEncodePath
Similar toURLEncoder.encode(String, String)
but doesn't encode"/" characters.- Parameters:
o
- The object to encode.- Returns:
- The URL encoded string, or
null if the object was null.
-
urlDecode
Decodes aapplication/x-www-form-urlencoded string usingUTF-8 encoding scheme.- Parameters:
s
- The string to decode.- Returns:
- The decoded string, or
null if input isnull .
-
urlEncode
Encodes aapplication/x-www-form-urlencoded string usingUTF-8 encoding scheme.- Parameters:
s
- The string to encode.- Returns:
- The encoded string, or
null if input isnull .
-
urlEncodeLax
Same asurlEncode(String)
except only escapes characters that absolutely need to be escaped.- Parameters:
s
- The string to escape.- Returns:
- The encoded string, or
null if input isnull .
-
firstNonWhitespaceChar
Returns the first non-whitespace character in the string.- Parameters:
s
- The string to check.- Returns:
- The first non-whitespace character, or
0 if the string isnull , empty, or composed of only whitespace.
-
lastNonWhitespaceChar
Returns the last non-whitespace character in the string.- Parameters:
s
- The string to check.- Returns:
- The last non-whitespace character, or
0 if the string isnull , empty, or composed of only whitespace.
-
charAt
Returns the character at the specified index in the string without throwing exceptions.- Parameters:
s
- The string.i
- The index position.- Returns:
- The character at the specified index, or
0 if the index is out-of-range or the string isnull .
-
isAbsoluteUri
Efficiently determines whether a URL is of the pattern "xxx://xxx"- Parameters:
s
- The string to test.- Returns:
true if it's an absolute path.
-
isUri
Efficiently determines whether a URL is of the pattern "xxx:/xxx".The pattern matched is:
[a-z]{2,}\:\/.* Note that this excludes filesystem paths such as
"C:/temp" .- Parameters:
s
- The string to test.- Returns:
true if it's an absolute path.
-
getAuthorityUri
Given an absolute URI, returns just the authority portion (e.g."http://hostname:port" )- Parameters:
s
- The URI string.- Returns:
- Just the authority portion of the URI.
-
toURI
Converts the specified object to a URI.- Parameters:
o
- The object to convert to a URI.- Returns:
- A new URI, or the same object if the object was already a URI, or
-
firstNonEmpty
Returns the first non-null, non-empty string in the list.- Parameters:
s
- The strings to test.- Returns:
- The first non-empty string in the list, or
null if they were allnull or empty.
-
indexOf
Same asString.indexOf(int)
except allows you to check for multiple characters.- Parameters:
s
- The string to check.c
- The characters to check for.- Returns:
- The index into the string that is one of the specified characters.
-
format
Similar toMessageFormat.format(String, Object...)
except allows you to specify POJO arguments.- Parameters:
pattern
- The string pattern.args
- The arguments.- Returns:
- The formatted string.
-
parseIntWithSuffix
Converts a string containing a possible multiplier suffix to an integer.The string can contain any of the following multiplier suffixes:
"K" - x 1024"M" - x 1024*1024"G" - x 1024*1024*1024"k" - x 1000"m" - x 1000*1000"g" - x 1000*1000*1000
- Parameters:
s
- The string to parse.- Returns:
- The parsed value.
-
parseLongWithSuffix
Converts a string containing a possible multiplier suffix to a long.The string can contain any of the following multiplier suffixes:
"K" - x 1024"M" - x 1024*1024"G" - x 1024*1024*1024"T" - x 1024*1024*1024*1024"P" - x 1024*1024*1024*1024*1024"k" - x 1000"m" - x 1000*1000"g" - x 1000*1000*1000"t" - x 1000*1000*1000*1000"p" - x 1000*1000*1000*1000*1000
- Parameters:
s
- The string to parse.- Returns:
- The parsed value.
-
contains
Same asString.contains(CharSequence)
except returnsnull if the value is null.- Parameters:
value
- The string to check.substring
- The value to check for.- Returns:
true if the value contains the specified substring.
-
isJsonArray
Returnstrue if the specified string appears to be an JSON array.- Parameters:
o
- The object to test.ignoreWhitespaceAndComments
- Iftrue , leading and trailing whitespace and comments will be ignored.- Returns:
true if the specified string appears to be a JSON array.
-
isJson
Returnstrue if the specified string is valid JSON.Leading and trailing spaces are ignored.
Leading and trailing comments are not allowed.- Parameters:
s
- The string to test.- Returns:
true if the specified string is valid JSON.
-
isJsonObject
Returnstrue if the specified string appears to be a JSON object.- Parameters:
o
- The object to test.ignoreWhitespaceAndComments
- Iftrue , leading and trailing whitespace and comments will be ignored.- Returns:
true if the specified string appears to be a JSON object.
-
getNumberedLines
Takes in a string, splits it by lines, and then prepends each line with line numbers.- Parameters:
s
- The string.- Returns:
- The string with line numbers added.
-
getNumberedLines
Same asgetNumberedLines(String)
except only returns the specified lines.Out-of-bounds values are allowed and fixed.
- Parameters:
s
- The string.start
- The starting line (1-indexed).end
- The ending line (1-indexed).- Returns:
- The string with line numbers added.
-
compare
Compares two strings, but gracefully handlesnulls .- Parameters:
s1
- The first string.s2
- The second string.- Returns:
- The same as
String.compareTo(String)
.
-
firstChar
Returns the first character in the specified string.- Parameters:
s
- The string to check.- Returns:
- The first character in the string, or
0 if the string isnull or empty.
-
getMatchPattern
Converts a string containing"*" meta characters with a regular expression pattern.- Parameters:
s
- The string to create a pattern from.- Returns:
- A regular expression pattern.
-
getMatchPattern
Converts a string containing"*" meta characters with a regular expression pattern.- Parameters:
s
- The string to create a pattern from.flags
- Regular expression flags.- Returns:
- A regular expression pattern.
-
getDuration
Parses a duration string.Examples:
"1000" - 1000 milliseconds."10s" - 10 seconds."10 sec" - 10 seconds."10 seconds" - 10 seconds.
Use any of the following suffixes:
- None (time in milliseconds).
"s" /"sec" /"second" /"seconds" "m" /"min" /"minutes" /"seconds" "h" /"hour" /"hours" "d" /"day" /"days" "w" /"week" /"weeks"
Suffixes are case-insensitive.
Whitespace is ignored.- Parameters:
s
- The string to parse.- Returns:
- The time in milliseconds, or
-1 if the string is empty ornull .
-
stripInvalidHttpHeaderChars
Strips invalid characters such as CTRL characters from a string meant to be encoded as an HTTP header value.- Parameters:
s
- The string to strip chars from.- Returns:
- The string with invalid characters removed.
-
abbreviate
Abbreviates a String using ellipses.- Parameters:
in
- The input string.length
- The max length of the resulting string.- Returns:
- The abbreviated string.
-
splitMethodArgs
Splits the method arguments in the signature of a method.- Parameters:
s
- The arguments to split.- Returns:
- The split arguments.
-
fixUrl
Attempts to escape any invalid characters found in a URI.- Parameters:
in
- The URI to fix.- Returns:
- The fixed URI.
-
countChars
Counts the number of the specified character in the specified string.- Parameters:
s
- The string to check.c
- The character to check for.- Returns:
- The number of those characters or zero if the string was
null .
-
compress
Converts string into a GZipped input stream.- Parameters:
contents
- The contents to compress.- Returns:
- The input stream converted to GZip.
- Throws:
Exception
- Exception occurred.
-
decompress
Converts a GZipped input stream into a string.- Parameters:
is
- The contents to decompress.- Returns:
- The string.
- Throws:
Exception
- Exception occurred.
-
cdl
Converts the specified object to a comma-delimited list.- Parameters:
o
- The object to convert.- Returns:
- The specified object as a comma-delimited list.
-
isNumberChar
Returnstrue if the specified character is a valid number character.- Parameters:
c
- The character to check.- Returns:
true if the specified character is a valid number character.
-