Class StringUtils
- Version:
- 1.11
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic PatternA pattern to match decimal number values.static final PatternA pattern for matching{type-len}digeststyle digest strings.static PatternA pattern to match integer number values.static final PatternPattern to capture template variable names of the form{name}.static final PatternA pattern that matches any character not allowed insimpleIdValue(String).static final PatternA pattern that matches any _ at the start or end of a string. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringGet a comma-delimited string from a collection of objects.commaDelimitedStringToMap(String mapping) Get string Map via a comma-delimited String value.Get a Set via a comma-delimited string value.static final KeyValuePairdecodeBase64DigestComponents(String digest) Decode a digest string in the form{key}valueinto a key-value pair composed of digest (key) and salt (value) hex-encoded values.static StringdelimitedStringFromCollection(Collection<?> set, String delim) Get a delimited string from a collection of objects.static StringdelimitedStringFromMap(Map<?, ?> map) Get a delimited string from a map of objects.static StringdelimitedStringFromMap(Map<?, ?> map, String keyValueDelim, String pairDelim) Get a delimited string from a map of objects.delimitedStringToMap(String mapping, String recordDelim, String fieldDelim) Get a string Map via a delimited String value.delimitedStringToSet(String list, String delim) Get a string Set via a delimited String value.static StringexpandTemplateString(String source, Map<String, ?> variables) Replace variables in a string template with corresponding values.static String[]expressions(Pattern[] patterns) Create an array of expression strings from Pattern objects.static String[]Test if a string matches a pattern, returning the text along with any capture groups as an array if there is a match.static MatcherTest if a string matches any one of a list of patterns.static NumbernumberValue(String text) Parse a number value if possible.static booleanGet a boolean value from a String.static Pattern[]Create an array of regular expressions from strings.static final Stringsha256Base64Value(String propertyValue) Compute a Base64-encoded SHA-256 digest of a string value with a random salt.static final Stringsha256Base64Value(String propertyValue, byte[] salt) Compute a Base64-encoded SHA-256 digest of a string value with optional salt.static <K,V> Map<K, V> sha256MaskedMap(Map<K, V> map, Set<K> maskKeys) "Mask" a set of map values by replacing them with SHA-256 digest values.static StringsimpleIdValue(String text) Generate a "simple" ID out of a string.static intutf8length(CharSequence text) Calcualte the UTF-8 byte length of a given string.
-
Field Details
-
NAMES_PATTERN
Pattern to capture template variable names of the form{name}. -
INTEGER_PATTERN
A pattern to match integer number values.- Since:
- 1.11
-
DECIMAL_PATTERN
A pattern to match decimal number values.- Since:
- 1.11
-
DIGEST_PREFIX_PATTERN
A pattern for matching{type-len}digeststyle digest strings.- Since:
- 1.7
-
NOT_SIMPLE_ID_CHARACTER_PATTERN
A pattern that matches any character not allowed insimpleIdValue(String).- Since:
- 1.8
-
UNDERSCORE_PREFIX_OR_SUFFIX
A pattern that matches any _ at the start or end of a string.- Since:
- 1.8
-
-
Method Details
-
expandTemplateString
Replace variables in a string template with corresponding values.Template variables are encoded like
{name:default}where the:defaultpart is optional. Thenamevalue is treated as a key in the providedvariablesmap, and any corresponding value found is turned into a string and replaces the template variable in the resulting string. The optionaldefaultvalue, if provided, will be used as the variable value ifnameis not found invariables.Adapted from the
org.springframework.web.util.UriComponentsclass, mimicking URI path variable substitutions.- Parameters:
source- the template string to replace variables invariables- the variables- Returns:
- the string with variables replaced, or null if
sourceis null - Since:
- 1.4
-
commaDelimitedStringFromCollection
Get a comma-delimited string from a collection of objects.- Parameters:
set- the set- Returns:
- the comma-delimited string
- See Also:
-
delimitedStringFromCollection
Get a delimited string from a collection of objects.This will call the
Object.toString()method on each object in the set, using the natural iteration ordering of the set.No attempt to escape delimiters within the set's values is done.- Parameters:
set- the setdelim- the delimiter- Returns:
- the delimited string
-
delimitedStringFromMap
Get a delimited string from a map of objects.This will call
delimitedStringFromMap(Map, String, String)using a=key value delimiter and a,pair delimiter.- Parameters:
map- the map- Returns:
- the string
-
delimitedStringFromMap
Get a delimited string from a map of objects.This will call the
Object.toString()method on each key and value in the map, using the natural iteration ordering of the map. No attempt to escape delimiters within the map's values is done.- Parameters:
map- the mapkeyValueDelim- the delimited to use between keys and valuespairDelim- the delimiter to use betwen key/value pairs- Returns:
- the string
-
commaDelimitedStringToSet
Get a Set via a comma-delimited string value.- Parameters:
list- the comma-delimited string- Returns:
- the Set, or null if
listis null or an empty string - See Also:
-
delimitedStringToSet
Get a string Set via a delimited String value.The format of the
listString should be a delimited list of values. Whitespace is permitted around the delimiter, and will be stripped from the values. Whitespace is also trimmed from the start and end of the input string. The list order is preserved in the iteration order of the returned Set.- Parameters:
list- the delimited textdelim- the delimiter to split the list with- Returns:
- the Set, or null if
listis null or an empty string
-
commaDelimitedStringToMap
Get string Map via a comma-delimited String value.The format of the
mappingString should be:key=val[,key=val,...]
- Parameters:
mapping- the delimited text- Returns:
- the map, or null if
mappingis null or empty - See Also:
-
delimitedStringToMap
public static Map<String,String> delimitedStringToMap(String mapping, String recordDelim, String fieldDelim) Get a string Map via a delimited String value.The format of the
mappingString should be:key=val[,key=val,...]
The record and field delimiters are passed as parameters to this method. Whitespace is permitted around all delimiters, and will be stripped from the keys and values. Whitespace is also trimmed from the start and end of the input string.
- Parameters:
mapping- the delimited textrecordDelim- the key+value record delimiterfieldDelim- the key+value delimiter- Returns:
- the map, or null if
mappingis null or empty
-
patterns
Create an array of regular expressions from strings. Ifexpressionsis null or empty, the result will be null. Pass 0 forflagsif no special flags are desired.- Parameters:
expressions- the array of expressions to compile intoPatternobjectsflags- the Pattern flags to use, or 0 for no flags- Returns:
- the compiled regular expressions, in the same order as
expressions, or null if no expressions supplied - Throws:
PatternSyntaxException- If an expression's syntax is invalid
-
expressions
Create an array of expression strings from Pattern objects. Ifpatternsis null or empty, the result will be null.- Parameters:
patterns- the array of Pattern objects to convert to strings (may be null)- Returns:
- the string expressions, in the same order as
patterns, or null if no patterns supplied
-
matches
Test if a string matches any one of a list of patterns. Thepatternslist will be tested one at a time, in array order. The first result that matches will be returned. If no match is found, null is returned.- Parameters:
patterns- the patterns to test (may be null)text- the string to test (may be null)- Returns:
- a
Matcherthat matchestextor null if no match was found
-
parseBoolean
Get a boolean value from a String.This method is more generous than
Boolean.parseBoolean(String). The following values are considered true, all ignoring case:- 1
- t
- true
- y
- yes
All other values (or a missing value) is considered false.
- Parameters:
s- the string to parse as a boolean- Returns:
- the parsed boolean result
- Since:
- 1.6
-
sha256Base64Value
Compute a Base64-encoded SHA-256 digest of a string value with a random salt.- Parameters:
propertyValue- the current property value- Returns:
- a Base64 encoded SHA-256 digest with a
{SSHA-256}prefix - Since:
- 1.7
-
sha256Base64Value
Compute a Base64-encoded SHA-256 digest of a string value with optional salt.When salt is provided, the digest is computed from propertyValue + salt and then the returned Base64 value contains digest + salt. The length of the salt can be determined after decoding the Base64 value, as decodedLength - 32.
- Parameters:
propertyValue- the current property valuesalt- the optional salt to add- Returns:
- a Base64 encoded SHA-256 digest with a
{SSHA-256}prefix (if salt provided) or{SHA-256}if no salt provided - Since:
- 1.7
-
decodeBase64DigestComponents
Decode a digest string in the form{key}valueinto a key-value pair composed of digest (key) and salt (value) hex-encoded values.The returned pair's
valuewill be null if no salt was included in the digest. Both values will be returned as hex-encoded strings.- Parameters:
digest- a Base64-encoded digest string, in the form returned bysha256Base64Value(String)- Returns:
- a key/value pair of the
- Since:
- 1.7
-
sha256MaskedMap
"Mask" a set of map values by replacing them with SHA-256 digest values.This method will return a new map instance, unless no values need masking in which case
mapitself will be returned. For any key inmaskKeysfound inmap, the returned map's value will be the SHA-256 digest value computed from the string form of the value passed tosha256Base64Value(String).- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the map of values to maskmaskKeys- the set of map keys whose values should be masked- Returns:
- either a new map instance with one or more values masked, or
mapwhen no values need masking - Since:
- 1.7
- See Also:
-
simpleIdValue
Generate a "simple" ID out of a string.A simple ID is created by taking
textand:- leading and trailing whitespace is removed
- change to lower case
- replace any runs of characters other than a-zA-Z0-9_ with a _
- Parameters:
text- the text to derive the simple ID from- Returns:
- the simple ID, or null if
textis null - Since:
- 1.8
-
utf8length
Calcualte the UTF-8 byte length of a given string.This is faster than converting the string to a byte array in the UTF-8 encoding.
- Parameters:
text- the text to calcualte the UTF-8 length of- Returns:
- the length
- Since:
- 1.10
-
numberValue
Parse a number value if possible.This method will return either a
BigIntegerorBigDecimalvalue.- Parameters:
text- the string to parse- Returns:
- a number instance if
textcan be parsed as a number, or null otherwise - Since:
- 1.11
-
match
Test if a string matches a pattern, returning the text along with any capture groups as an array if there is a match.Note that
Matcher.find()is used, so the pattern matches anywhere intextby default.- Parameters:
pattern- the patterntext- the string to test againstpattern- Returns:
- if the string does not match or either argument is
null, null; otherwise an array whose first
element is
textand any additional elements are pattern capture values - Since:
- 1.11
-