public final class StringUtils extends Object
Constructor and Description |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
asString(Object o)
Calls
Object.toString() on the specified object if it's not null. |
static String[] |
asStrings(Object... o)
Converts an array of objects to an array of strings.
|
static byte[] |
base64Decode(String in)
BASE64-decodes the specified string.
|
static String |
base64DecodeToString(String in)
Shortcut for calling
base64Decode(String) and converting the result to a UTF-8 encoded string. |
static String |
base64Encode(byte[] in)
BASE64-encodes the specified byte array.
|
static String |
base64EncodeToString(String in)
Shortcut for calling
base64Encode(in.getBytes( |
static char |
charAt(String s,
int i)
Returns the character at the specified index in the string without throwing exceptions.
|
static boolean |
contains(String value,
CharSequence substring)
Same as
String.contains(CharSequence) except returns |
static boolean |
containsAny(String s,
char... chars)
Returns
|
static String |
decodeHex(String s)
Debug method for rendering non-ASCII character sequences.
|
static String |
emptyIfNull(String s)
Returns an empty string if the specified string is
|
static boolean |
endsWith(String s,
char... c)
Same as
endsWith(String, char) except check for multiple characters. |
static boolean |
endsWith(String s,
char c)
An efficient method for checking if a string ends with a character.
|
static String |
firstNonEmpty(String... s)
Returns the first non-null, non-empty string in the list.
|
static char |
firstNonWhitespaceChar(String s)
Returns the first non-whitespace character in the string.
|
static String |
format(String pattern,
Object... args)
Similar to
MessageFormat.format(String, Object...) except allows you to specify POJO arguments. |
static byte[] |
fromHex(String hex)
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 as
fromHex(String) except expects spaces between the byte strings. |
static String |
fromSpacedHexToUTF8(String hex)
Converts a space-deliminted hexadecimal byte stream (e.g.
|
static String |
generateUUID(int numchars)
Generated a random UUID with the specified number of characters.
|
static String |
getAuthorityUri(String s)
Given an absolute URI, returns just the authority portion (e.g.
|
static String |
getField(int fieldNum,
String s,
char delim)
Returns the specified field in a delimited string without splitting the string.
|
static String |
getField(int fieldNum,
String s,
char delim,
String def)
Same as
getField(int, String, char) except allows you to specify the default value. |
static String |
getStackTrace(Throwable t)
Convenience method for getting a stack trace as a string.
|
static int |
indexOf(String s,
char... c)
Same as
String.indexOf(int) except allows you to check for multiple characters. |
static boolean |
isAbsoluteUri(String s)
Efficiently determines whether a URL is of the pattern "xxx://xxx"
|
static boolean |
isDecimal(String s)
Returns
|
static boolean |
isEmpty(Object s)
|
static boolean |
isEmpty(String s)
Returns
|
static boolean |
isEquals(String s1,
String s2)
Tests two strings for equality, but gracefully handles nulls.
|
static boolean |
isFirstNumberChar(char c)
Returns
|
static boolean |
isFloat(String s)
Returns
|
static boolean |
isNumeric(String s)
Returns
parseNumber(String, Class) . |
static boolean |
isObjectList(Object o)
Returns
|
static boolean |
isObjectMap(Object o)
Returns
|
static boolean |
isOneOf(String s,
String... values)
Returns
|
static boolean |
isUri(String s)
Efficiently determines whether a URL is of the pattern "xxx:/xxx".
|
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(int[] tokens,
char d)
Join the specified tokens into a delimited string.
|
static String |
join(int[] tokens,
String d)
Join the specified tokens into a delimited string.
|
static String |
join(Object[] tokens,
char d)
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(Object[] tokens,
String separator)
Join the specified tokens into a delimited string.
|
static char |
lastNonWhitespaceChar(String s)
Returns the last non-whitespace character in the string.
|
static String |
nullIfEmpty(String s)
Returns
|
static int |
parseIntWithSuffix(String s)
Converts a string containing a possible multiplier suffix to an integer.
|
static Date |
parseISO8601Date(String date)
Parses an ISO8601 string into a date.
|
static long |
parseLongWithSuffix(String s)
Converts a string containing a possible multiplier suffix to a long.
|
static Number |
parseNumber(ParserReader r,
Class<? extends Number> type)
Parses a number from the specified reader stream.
|
static Number |
parseNumber(String s,
Class<? extends Number> type)
Parses a number from the specified string.
|
static String |
parseNumberString(ParserReader r)
Reads a numeric string from the specified reader.
|
static boolean |
pathStartsWith(String path,
String pathPrefix)
Returns
|
static boolean |
pathStartsWith(String path,
String[] pathPrefixes)
Same as
pathStartsWith(String, String) but returns |
static String |
repeat(int count,
String pattern)
Creates a repeated pattern.
|
static String |
replaceUnicodeSequences(String s)
Replaces
|
static String |
replaceVars(String s,
Map<String,Object> m)
Simple utility for replacing variables of the form
|
static String[] |
split(String s)
Shortcut for calling
split(s, |
static String[] |
split(String[] s,
char c)
Same as
split(String, char) except splits all strings in the input and returns a single result. |
static String[] |
split(String s,
char c)
Splits a character-delimited string into a string array.
|
static String[] |
split(String s,
char c,
int limit)
Same as
split(String, char) but limits the number of tokens returned. |
static List<String> |
splitEqually(String s,
int size)
Splits a string into equally-sized parts.
|
static Map<String,String> |
splitMap(String s,
char delim,
char eq,
boolean trim)
Splits a list of key-value pairs into an ordered map.
|
static String[] |
splitQuoted(String s)
Splits a space-delimited string with optionally quoted arguments.
|
static boolean |
startsWith(String s,
char c)
An efficient method for checking if a string starts with a character.
|
static String |
toHex(byte[] bytes)
Converts a byte array into a simple hexadecimal character string.
|
static char[] |
toHex(int num)
Converts the specified number into a 4 hexadecimal characters.
|
static String |
toSpacedHex(byte[] bytes)
Same as
toHex(byte[]) but puts spaces between the byte strings. |
static URI |
toURI(Object o)
Converts the specified object to a URI.
|
static String |
trim(String s)
Same as
String.trim() but prevents NullPointerExceptions . |
static String |
trimEnd(String s)
Trims whitespace characters from the end of the specified string.
|
static String |
trimSlashes(String s)
Trims
|
static String |
trimStart(String s)
Trims whitespace characters from the beginning of the specified string.
|
static String |
trimTrailingSlashes(String s)
Trims
|
static StringBuffer |
trimTrailingSlashes(StringBuffer s)
Trims
|
static String |
unEscapeChars(String s,
char[] toEscape)
Removes escape characters (\) from the specified characters.
|
static String |
unEscapeChars(String s,
char[] toEscape,
char escapeChar)
Removes escape characters (specified by escapeChar) from the specified characters.
|
static String |
unicodeSequence(char c)
Creates an escaped-unicode sequence (e.g.
|
static String |
urlDecode(String s)
Decodes a
application/x-www-form-urlencoded string using UTF-8 encoding scheme. |
static String |
urlEncode(Object o)
Shortcut for calling
URLEncoder. . |
static String |
urlEncode(String s)
Encodes a
application/x-www-form-urlencoded string using UTF-8 encoding scheme. |
static String |
urlEncodePath(Object o)
Similar to
urlEncode(Object) but doesn't encode |
public StringUtils()
public static Number parseNumber(ParserReader r, Class<? extends Number> type) throws Exception
r
- The reader to parse the string from.type
- The number type to created.
Can be any of the following:
IOException
- If a problem occurred trying to read from the reader.Exception
public static String parseNumberString(ParserReader r) throws Exception
r
- The reader to read form.Exception
public static Number parseNumber(String s, Class<? extends Number> type) throws ParseException
s
- The string to parse the number from.type
- The number type to created.
Can be any of the following:
Number
, uses the best guess.ParseException
public static boolean isNumeric(String s)
parseNumber(String, Class)
.s
- The string to check.public static boolean isFirstNumberChar(char c)
c
- The character to test.public static boolean isFloat(String s)
s
- The string to check.public static boolean isDecimal(String s)
s
- The string to check.public static String getStackTrace(Throwable t)
t
- The throwable to get the stack trace from.t.printStackTrace()
public static String join(Object[] tokens, String separator)
tokens
- The tokens to join.separator
- The delimiter.tokens
is public static String join(int[] tokens, String d)
tokens
- The tokens to join.d
- The delimiter.tokens
is public static String join(Collection<?> tokens, String d)
tokens
- The tokens to join.d
- The delimiter.tokens
is public static StringBuilder join(Collection<?> tokens, String d, StringBuilder sb)
tokens
- The tokens to join.d
- The delimiter.sb
- The string builder to append the response to.sb
.public static String join(Object[] tokens, char d)
tokens
- The tokens to join.d
- The delimiter.tokens
is public static StringBuilder join(Object[] tokens, char d, StringBuilder sb)
tokens
- The tokens to join.d
- The delimiter.sb
- The string builder to append the response to.sb
.public static String join(int[] tokens, char d)
tokens
- The tokens to join.d
- The delimiter.tokens
is public static String join(Collection<?> tokens, char d)
tokens
- The tokens to join.d
- The delimiter.tokens
is public static String[] split(String s)
split(s, ',' )
s
- The string to split. Can be public static String[] split(String s, char c)
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"}
s
- The string to split. Can be c
- The character to split on.public static String[] split(String s, char c, int limit)
split(String, char)
but limits the number of tokens returned.s
- The string to split. Can be c
- The character to split on.limit
- The maximum number of tokens to return.public static String[] split(String[] s, char c)
split(String, char)
except splits all strings in the input and returns a single result.s
- The string to split. Can be c
- The character to split on.public static Map<String,String> splitMap(String s, char delim, char eq, boolean trim)
Example:
String in =
s
- The string to split.delim
- The delimiter between the key-value pairs.eq
- The delimiter between the key and value.trim
- Trim strings after parsing.public static boolean containsAny(String s, char... chars)
s
- The string to test.chars
- The characters to look for.public static String[] splitQuoted(String s)
Examples:
["foo"]
["foo"]
["foo","bar","baz"]
["foo","bar baz"]
["foo","bar baz"]
["foo","bar'baz"]
s
- The input string.public static boolean isEmpty(String s)
s
- The string to check.public static boolean isEmpty(Object s)
s
- The string to check.Object.toString()
method returns an empty string.public static String nullIfEmpty(String s)
s
- The string to check.public static String emptyIfNull(String s)
s
- The string to check.public static String unEscapeChars(String s, char[] toEscape)
s
- The string to remove escape characters from.toEscape
- The characters escaped.public static String unEscapeChars(String s, char[] toEscape, char escapeChar)
s
- The string to remove escape characters from.toEscape
- The characters escaped.escapeChar
- The escape character.public static String decodeHex(String s)
s
- The string to decode.public static boolean startsWith(String s, char c)
s
- The string to check. Can be c
- The character to check for.public static boolean endsWith(String s, char c)
s
- The string to check. Can be c
- The character to check for.public static boolean endsWith(String s, char... c)
endsWith(String, char)
except check for multiple characters.s
- The string to check. Can be c
- The characters to check for.public static final char[] toHex(int num)
num
- The number to convert to hex.char [4]
containing the specified characters.public static boolean isEquals(String s1, String s2)
s1
- String 1.s2
- String 2.public static String base64EncodeToString(String in)
base64Encode(in.getBytes("UTF-8" ))
in
- The input string to convert.public static String base64Encode(byte[] in)
in
- The input byte array to convert.public static String base64DecodeToString(String in)
base64Decode(String)
and converting the result to a UTF-8 encoded string.in
- The BASE-64 encoded string to decode.public static byte[] base64Decode(String in)
in
- The BASE-64 encoded string.public static String generateUUID(int numchars)
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
numchars
- The number of characters in the generated UUID.public static String trim(String s)
String.trim()
but prevents NullPointerExceptions
.s
- The string to trim.public static Date parseISO8601Date(String date) throws IllegalArgumentException
date
- The date string.IllegalArgumentException
public static String replaceVars(String s, Map<String,Object> m)
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.
s
- The string containing variables to replace.m
- The map containing the variable values.public static boolean pathStartsWith(String path, String pathPrefix)
pathStartsWith(
path
- The path to check.pathPrefix
- The prefix.public static boolean pathStartsWith(String path, String[] pathPrefixes)
pathStartsWith(String, String)
but returns path
- The path to check.pathPrefixes
- The prefixes.public static String replaceUnicodeSequences(String s)
s
- The string to replace unicode sequences in.public static String unicodeSequence(char c)
c
- The character to create a sequence for.public static String getField(int fieldNum, String s, char delim)
Equivalent to the following:
String in =
fieldNum
- The field number. Zero-indexed.s
- The input string.delim
- The delimiter character.public static String getField(int fieldNum, String s, char delim, String def)
getField(int, String, char)
except allows you to specify the default value.fieldNum
- The field number. Zero-indexed.s
- The input string.delim
- The delimiter character.def
- The default value if the field does not exist.public static String asString(Object o)
Object.toString()
on the specified object if it's not null.o
- The object to convert to a string.public static String[] asStrings(Object... o)
o
- The array of objects to convert to strings.public static String fromHexToUTF8(String hex)
hex
- The hexadecimal string.public static String fromSpacedHexToUTF8(String hex)
hex
- The hexadecimal string.public static String toHex(byte[] bytes)
bytes
- The bytes to convert to hexadecimal.public static String toSpacedHex(byte[] bytes)
toHex(byte[])
but puts spaces between the byte strings.bytes
- The bytes to convert to hexadecimal.public static byte[] fromHex(String hex)
hex
- The string to convert to a byte array.public static byte[] fromSpacedHex(String hex)
fromHex(String)
except expects spaces between the byte strings.hex
- The string to convert to a byte array.public static String repeat(int count, String pattern)
count
- The number of times to repeat the pattern.pattern
- The pattern to repeat.public static String trimStart(String s)
s
- The string to trim.public static String trimEnd(String s)
s
- The string to trim.public static boolean isOneOf(String s, String... values)
s
- The string to test.
Can be values
- The values to test.
Can contain public static String trimSlashes(String s)
s
- The string to trim.public static String trimTrailingSlashes(String s)
s
- The string to trim.public static StringBuffer trimTrailingSlashes(StringBuffer s)
s
- The string to trim.public static String urlEncode(Object o)
URLEncoder.encode (o.toString(), "UTF-8" )
.o
- The object to encode.public static String urlEncodePath(Object o)
urlEncode(Object)
but doesn't encode o
- The object to encode.public static String urlDecode(String s)
application/x-www-form-urlencoded
string using UTF-8
encoding scheme.s
- The string to decode.public static String urlEncode(String s)
application/x-www-form-urlencoded
string using UTF-8
encoding scheme.s
- The string to encode.public static List<String> splitEqually(String s, int size)
s
- The string to split.size
- The token sizes.public static char firstNonWhitespaceChar(String s)
s
- The string to check.0
if the string is public static char lastNonWhitespaceChar(String s)
s
- The string to check.0
if the string is public static char charAt(String s, int i)
s
- The string.i
- The index position.0
if the index is out-of-range or the string
is public static boolean isAbsoluteUri(String s)
s
- The string to test.public static boolean isUri(String s)
The pattern matched is: [a-z]{2,}\:\/.*
Note that this excludes filesystem paths such as
s
- The string to test.public static String getAuthorityUri(String s)
s
- The URI string.public static URI toURI(Object o)
o
- The object to convert to a URI.public static String firstNonEmpty(String... s)
s
- The strings to test.public static int indexOf(String s, char... c)
String.indexOf(int)
except allows you to check for multiple characters.s
- The string to check.c
- The characters to check for.public static String format(String pattern, Object... args)
MessageFormat.format(String, Object...)
except allows you to specify POJO arguments.pattern
- The string pattern.args
- The arguments.public static int parseIntWithSuffix(String s)
The string can contain any of the following multiplier suffixes:
s
- The string to parse.public static long parseLongWithSuffix(String s)
The string can contain any of the following multiplier suffixes:
s
- The string to parse.public static boolean contains(String value, CharSequence substring)
String.contains(CharSequence)
except returns value
- The string to check.substring
- The value to check for.public static boolean isObjectList(Object o)
o
- The object to test.public static boolean isObjectMap(Object o)
o
- The object to test.Copyright © 2018 Apache. All rights reserved.