Class StringUtil
Strings.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringabbreviate(String str, int maxWidth) Abbreviates a String using ellipses.static Stringabbreviate(String str, int offset, int maxWidth) Abbreviates a String using ellipses.static booleanareStringEquals(String s1, String s2) Indicates if two Strings are equals, managing null.static StringasBase64(byte[] binaryData) Encodes the specified binary data into a text of Base64 characters.static floatDecodes the specified textual value as a float real.static intDecodes the specified textual value as an integer.static Stringcapitalize(String str) Capitalizes a String changing the first character to title case as perCharacter.toTitleCase(int).static StringconvertToEncoding(String toConvert, String encoding) Converts the given text into the specified charset.static <T extends CharSequence>
TdefaultIfBlank(T str, T defaultStr) Returns either the passed in CharSequence, or if the CharSequence is whitespace, empty ("") ornull, the value ofdefaultStr.static StringdefaultStringIfNotDefined(String string) Returns either the passed in String, or if the String isnot defined, an empty String ("").static StringdefaultStringIfNotDefined(String string, String defaultString) Returns either the passed in String, or if the String isnot defined, the value ofdefaultString.definedString(String string) This method allows the caller to handle the case where a string is not defined in a functional way.static StringdoubleAntiSlash(String path) Doubles the anti-slash character in a path value.static StringGets an empty string as a constant (reusable empty string).static StringescapeQuote(String text) static StringFormat a string by extending the principle of the method format() of the class java.text.MessageFormat to string arguments.static byte[]fromBase64(String base64Text) Decodes the specified text with Base64 characters in binary.static booleangetBooleanValue(String expression) Evaluate the expression and return true if expression equals "true", "yes", "y", "1" or "oui".static booleanIs the specified string is well-defined?static booleanIs the specified value encodes aFloat?static booleanIs the specified value encodes anInteger?static booleanIs the specified value encodes aLong?static booleanisNotDefined(String str) Is the specified string is not defined?static booleanisValidHour(String time) Is the specified text represents a time in the format expected by Silverpeas.static StringLeft pad a String with a specified String.static booleanIs the actual value is like the expected one?static booleanlikeIgnoreCase(String actualValue, String expectedValue) Is the actual value is like the expected one?static StringNormalizes the given string (which must be encoded into UTF-8) in order that the result contains only unified chars.static StringnormalizeByRemovingAccent(String string) Same treatment as the one ofnormalize(String)but removes also the accented characters.static StringregexReplace(String source, String regex, String replacement) Replace parts of a text by a replacement string.static voidrequireDefined(String str) Requires the specified string to be defined, otherwise anAssertionErroris thrown.static voidrequireDefined(String str, String message) Requires the specified string to be defined, otherwise anAssertionErroris thrown with the given message.static <T extends SilverpeasException>
voidrequireDefined(String str, Supplier<T> exceptionSupplier) Requires the specified string to be defined, otherwise throws the supplied exception.static String[]Splits the provided text into an array, separator specified.static String[]Splits the provided text into an array, separators specified.static String[]splitByWholeSeparator(String str, String separator) Splits the provided text into an array, separator string specified.splitString(String str, char separatorChar) Splits the provided text into an array, separator specified.static booleanstartsWith(CharSequence str, CharSequence prefix) Check if a CharSequence starts with a specified prefix.static booleanstartsWithIgnoreCase(String str, String prefix) Case insensitive check if a CharSequence starts with a specified prefix.static StringtoAcceptableFilename(String name) Replaces all quotes by spaces.static Stringstatic Stringuncapitalize(String str) Uncapitalizes a String, changing the first character to lower case as perCharacter.toLowerCase(int).
-
Field Details
-
EMPTY
- See Also:
-
NEWLINE
-
-
Method Details
-
emptyString
Gets an empty string as a constant (reusable empty string).- Returns:
- an empty string.
-
startsWithIgnoreCase
Case insensitive check if a CharSequence starts with a specified prefix.
nulls are handled without exceptions. Twonullreferences are considered to be equal. The comparison is case insensitive.StringUtils.startsWithIgnoreCase(null, null) = true StringUtils.startsWithIgnoreCase(null, "abc") = false StringUtils.startsWithIgnoreCase("abcdef", null) = false StringUtils.startsWithIgnoreCase("abcdef", "abc") = true StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true- Parameters:
str- the text to check, may be nullprefix- the text to find, may be null- Returns:
trueif the CharSequence starts with the prefix, case-insensitive, or bothnull- See Also:
-
abbreviate
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "...is the time for..."
Works like
abbreviate(String, int), but allows you to specify a "left edge" offset. Note that this left edge is not necessarily going to be the leftmost character in the result, or the first character following the ellipses, but it will appear somewhere in the result.In no case will it return a String of length greater than
maxWidth.StringUtils.abbreviate(null, *, *) = null StringUtils.abbreviate("", 0, 4) = "" StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 5, 10) = "...fghi..." StringUtils.abbreviate("abcdefghijklmno", 6, 10) = "...ghij..." StringUtils.abbreviate("abcdefghijklmno", 8, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghij", 0, 3) = IllegalArgumentException StringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException- Parameters:
str- the String to check, may be nulloffset- left edge of source StringmaxWidth- maximum length of result String, must be at least 4- Returns:
- abbreviated String,
nullif null String input - Throws:
IllegalArgumentException- if the width is too small
-
abbreviate
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "Now is the time for..."
Specifically:
- If the number of characters in
stris less than or equal tomaxWidth, returnstr. - Else abbreviate it to
(substring(str, 0, max-3) + "..."). - If
maxWidthis less than4, throw anIllegalArgumentException. - In no case will it return a String of length greater than
maxWidth.
StringUtils.abbreviate(null, *) = null StringUtils.abbreviate("", 4) = "" StringUtils.abbreviate("abcdefg", 6) = "abc..." StringUtils.abbreviate("abcdefg", 7) = "abcdefg" StringUtils.abbreviate("abcdefg", 8) = "abcdefg" StringUtils.abbreviate("abcdefg", 4) = "a..." StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException- Parameters:
str- the String to check, may be nullmaxWidth- maximum length of result String, must be at least 4- Returns:
- abbreviated String,
nullif null String input - Throws:
IllegalArgumentException- if the width is too small
- If the number of characters in
-
leftPad
Left pad a String with a specified String.
Pad to a size of
size.StringUtils.leftPad(null, *, *) = null StringUtils.leftPad("", 3, "z") = "zzz" StringUtils.leftPad("bat", 3, "yz") = "bat" StringUtils.leftPad("bat", 5, "yz") = "yzbat" StringUtils.leftPad("bat", 8, "yz") = "yzyzybat" StringUtils.leftPad("bat", 1, "yz") = "bat" StringUtils.leftPad("bat", -1, "yz") = "bat" StringUtils.leftPad("bat", 5, null) = " bat" StringUtils.leftPad("bat", 5, "") = " bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadStr- the String to pad with, null or empty treated as single space- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input
-
defaultIfBlank
Returns either the passed in CharSequence, or if the CharSequence is whitespace, empty ("") or
null, the value ofdefaultStr.Whitespace is defined by
Character.isWhitespace(char).StringUtils.defaultIfBlank(null, "NULL") = "NULL" StringUtils.defaultIfBlank("", "NULL") = "NULL" StringUtils.defaultIfBlank(" ", "NULL") = "NULL" StringUtils.defaultIfBlank("bat", "NULL") = "bat" StringUtils.defaultIfBlank("", null) = null- Type Parameters:
T- the specific kind of CharSequence- Parameters:
str- the CharSequence to check, may be nulldefaultStr- the default CharSequence to return if the input is whitespace, empty ("") ornull, may be null- Returns:
- the passed in CharSequence, or the default
- See Also:
-
isDefined
Is the specified string is well-defined?- Parameters:
str- a string to check.- Returns:
- true if the given string isn't null, nor empty and doesn't contain any space characters. False otherwise. The "null" string is considered as a non-defined string.
-
isNotDefined
Is the specified string is not defined?- Parameters:
str- the string to check.- Returns:
- true if the string is either null, an empty string, or a string containing only space characters. False otherwise. The "null" string is considered as a non-defined string.
- See Also:
- Implementation Note:
- it is the reverse of the
isDefined(String)method.
-
requireDefined
Requires the specified string to be defined, otherwise anAssertionErroris thrown.- Parameters:
str- the string to check.
-
requireDefined
Requires the specified string to be defined, otherwise anAssertionErroris thrown with the given message.- Parameters:
str- the string to check.message- the message to pass if the string doesn't satisfy the requirement.
-
requireDefined
public static <T extends SilverpeasException> void requireDefined(String str, Supplier<T> exceptionSupplier) throws T Requires the specified string to be defined, otherwise throws the supplied exception.- Parameters:
str- the string to check.exceptionSupplier- a supplier of the Silverpeas exception to throw if the string isn't defined.- Throws:
T
-
startsWith
Check if a CharSequence starts with a specified prefix.
nulls are handled without exceptions. Twonullreferences are considered to be equal. The comparison is case sensitive.StringUtils.startsWith(null, null) = true StringUtils.startsWith(null, "abc") = false StringUtils.startsWith("abcdef", null) = false StringUtils.startsWith("abcdef", "abc") = true StringUtils.startsWith("ABCDEF", "abc") = false- Parameters:
str- the CharSequence to check, may be nullprefix- the prefix to find, may be null- Returns:
trueif the CharSequence starts with the prefix, case-sensitive, or bothnull- See Also:
-
splitByWholeSeparator
Splits the provided text into an array, separator string specified.
The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.
A
nullinput String returnsnull. Anullseparator splits on whitespace.StringUtils.splitByWholeSeparator(null, *) = null StringUtils.splitByWholeSeparator("", *) = [] StringUtils.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"] StringUtils.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"] StringUtils.splitByWholeSeparator("ab:cd:ef", ":") = ["ab", "cd", "ef"] StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]- Parameters:
str- the String to parse, may be nullseparator- String containing the String to be used as a delimiter,nullsplits on whitespace- Returns:
- an array of parsed Strings,
nullif null String was input
-
split
Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A
nullinput String returnsnull.StringUtils.split(null, *) = null StringUtils.split("", *) = [] StringUtils.split("a.b.c", '.') = ["a", "b", "c"] StringUtils.split("a..b.c", '.') = ["a", "b", "c"] StringUtils.split("a:b:c", '.') = ["a:b:c"] StringUtils.split("a b c", ' ') = ["a", "b", "c"]- Parameters:
str- the String to parse, may be nullseparatorChar- the character used as the delimiter- Returns:
- an array of parsed Strings,
nullif null String input - Since:
- 2.0
-
split
Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A
nullinput String returnsnull. AnullseparatorChars splits on whitespace.StringUtils.split(null, *) = null StringUtils.split("", *) = [] StringUtils.split("abc def", null) = ["abc", "def"] StringUtils.split("abc def", " ") = ["abc", "def"] StringUtils.split("abc def", " ") = ["abc", "def"] StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]- Parameters:
str- the String to parse, may be nullseparatorChars- the characters used as the delimiters,nullsplits on whitespace- Returns:
- an array of parsed Strings,
nullif null String input
-
capitalize
Capitalizes a String changing the first character to title case as per
Character.toTitleCase(int). No other characters are changed.A
nullinput String returnsnull.StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt" StringUtils.capitalize("'cat'") = "'cat'"- Parameters:
str- the String to capitalize, may be null- Returns:
- the capitalized String,
nullif null String input - See Also:
-
uncapitalize
Uncapitalizes a String, changing the first character to lower case as per
Character.toLowerCase(int). No other characters are changed.A
nullinput String returnsnull.StringUtils.uncapitalize(null) = null StringUtils.uncapitalize("") = "" StringUtils.uncapitalize("cat") = "cat" StringUtils.uncapitalize("Cat") = "cat" StringUtils.uncapitalize("CAT") = "cAT"- Parameters:
str- the String to uncapitalize, may be null- Returns:
- the uncapitalized String,
nullif null String input - See Also:
-
normalize
Normalizes the given string (which must be encoded into UTF-8) in order that the result contains only unified chars.Indeed, according to the environment of the user, sometimes it is sent data with combined characters which will make the server have a bad behavior, like throw an error on file download.
- Parameters:
string- the string to normalize. There is no guarantee when the string is not encoded into UTF8.- Returns:
- the normalized string.
-
normalizeByRemovingAccent
Same treatment as the one ofnormalize(String)but removes also the accented characters.- Parameters:
string- the string to normalize. There is no guarantee when the string is not encoded into UTF8.- Returns:
- the normalized string.
-
defaultStringIfNotDefined
Returns either the passed in String, or if the String is
not defined, an empty String ("").StringUtil.defaultStringIfNotDefined(null) = "" StringUtil.defaultStringIfNotDefined("") = "" StringUtil.defaultStringIfNotDefined(" ") = "" StringUtil.defaultStringIfNotDefined("bat") = "bat"- Parameters:
string- the String to check, may be null, blank or filled by spaces if the input isnot defined, may be null, blank or filled by spaces- Returns:
- the passed in String, or the default if it was
null - See Also:
-
defaultStringIfNotDefined
Returns either the passed in String, or if the String is
not defined, the value ofdefaultString.StringUtil.defaultStringIfNotDefined(null, "NULL") = "NULL" StringUtil.defaultStringIfNotDefined("", "NULL") = "NULL" StringUtil.defaultStringIfNotDefined(" ", "NULL") = "NULL" StringUtil.defaultStringIfNotDefined("bat", "NULL") = "bat"- Parameters:
string- the String to check, may be null, blank or filled by spacesdefaultString- the default String to return if the input isnot defined, may be null, blank or filled by spaces- Returns:
- the passed in String, or the default if it was
null - See Also:
-
definedString
This method allows the caller to handle the case where a string is not defined in a functional way.If the returned optional is present (so not empty), it means that the string given as parameter is defined (checked with
isDefined(String)).If the returned optional is not present (so empty), it means that the string given as parameter is not defined. In a such cas, thanks to
Optional, the caller can choose the behavior to adopt.- Parameters:
string- the String to check, may be null, blank or filled by spaces- Returns:
- optional with given string if defined
-
isInteger
Is the specified value encodes anInteger?- Parameters:
value- the textual representation of an integer.- Returns:
- true if the given value represents an integer. False otherwise.
-
asInt
Decodes the specified textual value as an integer.- Parameters:
value- the textual value to decode.defaultValue- the default value to use if the value doesn't encode an integer.- Returns:
- either the integer representation of the value or the given default value if it isn't an integer representation.
-
isLong
Is the specified value encodes aLong?- Parameters:
value- the textual representation of a long integer.- Returns:
- true if the given value represents a long integer. False otherwise.
-
asFloat
Decodes the specified textual value as a float real.- Parameters:
value- the textual value to decode.- Returns:
- either the float real representation of the value or 0 if it isn't a float real representation.
-
isFloat
Is the specified value encodes aFloat?- Parameters:
value- the textual representation of a float real.- Returns:
- true if the given value represents a float real. False otherwise.
-
escapeQuote
- Parameters:
text- the text from which the quotes must be escaped (replaced by spaces in fact).- Returns:
- a String with all quotes replaced by spaces
-
toAcceptableFilename
Replaces all quotes by spaces.- Parameters:
name- the original filename.- Returns:
- a String with all quotes replaced by spaces
-
format
Format a string by extending the principle of the method format() of the class java.text.MessageFormat to string arguments. For instance, the string '{key}' contained in the original string to format will be replaced by the value corresponding to this key contained into the values map.- Parameters:
label- The string to formatvalues- The values to insert into the string- Returns:
- The formatted string, filled with values of the map.
-
truncate
- Parameters:
text- The string to truncate if its size is greater than the maximum length given as parameter.maxLength- The maximum length required.- Returns:
- The truncated string followed by '...' if needed. Returns the string itself if its length is smaller than the required maximum length.
-
regexReplace
Replace parts of a text by a replacement string. The text to replace is specified by a regex.- Parameters:
source- the original textregex- the regex that permits to identify parts of text to replacereplacement- the replacement text- Returns:
- The source text modified
-
isValidHour
Is the specified text represents a time in the format expected by Silverpeas.- Parameters:
time- the textual representation of a time.- Returns:
- true if the specified parameter represents a time formatted in the format expected by Silverpeas.
- See Also:
-
convertToEncoding
Converts the given text into the specified charset. If the charset isn't supported, then anUnsupportedEncodingExceptionexception is thrown.- Parameters:
toConvert- the text to convert.encoding- the charset into which the text has to be converted.- Returns:
- the converted text.
-
getBooleanValue
Evaluate the expression and return true if expression equals "true", "yes", "y", "1" or "oui".- Parameters:
expression- the expression to be evaluated- Returns:
- true if expression equals "true", "yes", "y", "1" or "oui".
-
areStringEquals
Indicates if two Strings are equals, managing null.- Parameters:
s1- the first String.s2- the second String.- Returns:
- true if the two Strings are equals.
-
asBase64
Encodes the specified binary data into a text of Base64 characters.- Parameters:
binaryData- the binary data to convert in Base64-based String.- Returns:
- a String representation of the binary data in Base64 characters.
-
fromBase64
Decodes the specified text with Base64 characters in binary.- Parameters:
base64Text- the text in Base64.- Returns:
- the binary representation of the text.
-
splitString
Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A
nullinput String returnsnull.StringUtils.split(null, *) = null StringUtils.split("", *) = empty list StringUtils.split("a.b.c", '.') = ["a", "b", "c"] StringUtils.split("a..b.c", '.') = ["a", "b", "c"] StringUtils.split("a:b:c", '.') = ["a:b:c"] StringUtils.split("a b c", ' ') = ["a", "b", "c"]- Parameters:
str- the String to parse, may be nullseparatorChar- the character used as the delimiter- Returns:
- an array of parsed Strings,
nullif null String input
-
doubleAntiSlash
Doubles the anti-slash character in a path value.- Parameters:
path- the String representing a path.- Returns:
- the path corrected with the anti-slash doubled.
-
likeIgnoreCase
Is the actual value is like the expected one? Case-sensitivity isn't taken into account in the like-matching.- Parameters:
actualValue- the actual value to compare.expectedValue- the expected value the actual one has to match.- Returns:
- true if the actual value is like the expected one. False otherwise.
-
like
Is the actual value is like the expected one? Case-sensitivity of the values matters.- Parameters:
actualValue- the actual value to compare.expectedValue- the expected value the actual one has to match.- Returns:
- true if the actual value is like the expected one. False otherwise.
-