public final class StringUtils extends Object
Operations on String that are
 null safe.
The StringUtils class defines certain words related to
 String handling.
null"")' ', char 32)Character.isWhitespace(char)String.trim()StringUtils handles null input Strings quietly.
 That is to say that a null input will return null.
 Where a boolean or int is being returned
 details vary by method.
A side effect of the null handling is that a
 NullPointerException should be considered a bug in
 StringUtils.
This class's source was modified from the Apache commons-lang library: https://github.com/apache/commons-lang/
#ThreadSafe#
String| Modifier and Type | Method and Description | 
|---|---|
| static String | capitalize(String str)Capitalizes a String changing the first character to title case as
 per  Character.toTitleCase(int). | 
| static boolean | equals(String cs1,
      String cs2)Compares two Strings, returning  trueif they represent
 equal sequences of characters. | 
| static Character | findFirstOccurrence(String s,
                   char... charsToMatch)Searches a string for the first occurrence of a character specified by a list of characters. | 
| static String | fromBytes(byte[] bytes,
         Charset charset)Encode the given bytes as a string using the given charset | 
| static boolean | isBlank(CharSequence cs)Checks if a CharSequence is empty (""), null or whitespace only. | 
| static boolean | isEmpty(CharSequence cs)Checks if a CharSequence is empty ("") or null. | 
| static boolean | isNotBlank(CharSequence cs)Checks if a CharSequence is not empty (""), not null and not whitespace only. | 
| static String | lowerCase(String str)Converts a String to lower case as per  String.toLowerCase(). | 
| static String | repeat(String value,
      int count)Returns a string whose value is the concatenation of this string repeated  counttimes. | 
| static String | replace(String text,
       String searchString,
       String replacement)Replaces a String with another String inside a larger String,
 for the first  maxvalues of the search String,
 case sensitively/insensitively based onignoreCasevalue. | 
| static String | replaceEach(String text,
           String[] searchList,
           String[] replacementList)
 Replaces all occurrences of Strings within another String. | 
| static String | replaceOnce(String text,
           String searchString,
           String replacement)Replaces a String with another String inside a larger String, once. | 
| static String | replacePrefixIgnoreCase(String str,
                       String prefix,
                       String replacement)Replace the prefix of the string provided ignoring case considerations. | 
| static boolean | safeStringToBoolean(String value)Convert a string to boolean safely (as opposed to the less strict  Boolean.parseBoolean(String)). | 
| static boolean | startsWithIgnoreCase(String str,
                    String prefix)Tests if this string starts with the specified prefix ignoring case considerations. | 
| static String | substring(String str,
         int start)Gets a substring from the specified String avoiding exceptions. | 
| static String | substring(String str,
         int start,
         int end)Gets a substring from the specified String avoiding exceptions. | 
| static String | trim(String str)Removes control characters (char <= 32) from both
 ends of this String, handling  nullby returningnull. | 
| static String | trimToEmpty(String str)Removes control characters (char <= 32) from both
 ends of this String returning an empty String ("") if the String
 is empty ("") after the trim or if it is  null. | 
| static String | trimToNull(String str)Removes control characters (char <= 32) from both
 ends of this String returning  nullif the String is
 empty ("") after the trim or if it isnull. | 
| static String | uncapitalize(String str)Uncapitalizes a String, changing the first character to lower case as
 per  Character.toLowerCase(int). | 
| static String | upperCase(String str)Converts a String to upper case as per  String.toUpperCase(). | 
public static boolean isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("bob")     = false
 StringUtils.isEmpty("  bob  ") = false
 
 NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
cs - the CharSequence to check, may be nulltrue if the CharSequence is empty or nullpublic static boolean isBlank(CharSequence cs)
Checks if a CharSequence is empty (""), null or whitespace only.
Whitespace is defined by Character.isWhitespace(char).
 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false
 cs - the CharSequence to check, may be nulltrue if the CharSequence is null, empty or whitespace onlypublic static boolean isNotBlank(CharSequence cs)
Checks if a CharSequence is not empty (""), not null and not whitespace only.
Whitespace is defined by Character.isWhitespace(char).
 StringUtils.isNotBlank(null)      = false
 StringUtils.isNotBlank("")        = false
 StringUtils.isNotBlank(" ")       = false
 StringUtils.isNotBlank("bob")     = true
 StringUtils.isNotBlank("  bob  ") = true
 cs - the CharSequence to check, may be nulltrue if the CharSequence is not empty and not null and not whitespace onlypublic static String trim(String str)
Removes control characters (char <= 32) from both
 ends of this String, handling null by returning
 null.
The String is trimmed using String.trim().
 Trim removes start and end characters <= 32.
 StringUtils.trim(null)          = null
 StringUtils.trim("")            = ""
 StringUtils.trim("     ")       = ""
 StringUtils.trim("abc")         = "abc"
 StringUtils.trim("    abc    ") = "abc"
 str - the String to be trimmed, may be nullnull if null String inputpublic static String trimToNull(String str)
Removes control characters (char <= 32) from both
 ends of this String returning null if the String is
 empty ("") after the trim or if it is null.
 
The String is trimmed using String.trim().
 Trim removes start and end characters <= 32.
 StringUtils.trimToNull(null)          = null
 StringUtils.trimToNull("")            = null
 StringUtils.trimToNull("     ")       = null
 StringUtils.trimToNull("abc")         = "abc"
 StringUtils.trimToNull("    abc    ") = "abc"
 str - the String to be trimmed, may be nullnull if only chars <= 32, empty or null String inputpublic static String trimToEmpty(String str)
Removes control characters (char <= 32) from both
 ends of this String returning an empty String ("") if the String
 is empty ("") after the trim or if it is null.
 
The String is trimmed using String.trim().
 Trim removes start and end characters <= 32.
 StringUtils.trimToEmpty(null)          = ""
 StringUtils.trimToEmpty("")            = ""
 StringUtils.trimToEmpty("     ")       = ""
 StringUtils.trimToEmpty("abc")         = "abc"
 StringUtils.trimToEmpty("    abc    ") = "abc"
 str - the String to be trimmed, may be nullnull inputpublic static boolean equals(String cs1, String cs2)
Compares two Strings, returning true if they represent
 equal sequences of characters.
nulls are handled without exceptions. Two null
 references are considered to be equal. The comparison is case sensitive.
 StringUtils.equals(null, null)   = true
 StringUtils.equals(null, "abc")  = false
 StringUtils.equals("abc", null)  = false
 StringUtils.equals("abc", "abc") = true
 StringUtils.equals("abc", "ABC") = false
 cs1 - the first String, may be nullcs2 - the second String, may be nulltrue if the Strings are equal (case-sensitive), or both nullObject.equals(Object)public static String substring(String str, int start)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start n
 characters from the end of the String.
A null String will return null.
 An empty ("") String will return "".
 StringUtils.substring(null, *)   = null
 StringUtils.substring("", *)     = ""
 StringUtils.substring("abc", 0)  = "abc"
 StringUtils.substring("abc", 2)  = "c"
 StringUtils.substring("abc", 4)  = ""
 StringUtils.substring("abc", -2) = "bc"
 StringUtils.substring("abc", -4) = "abc"
 str - the String to get the substring from, may be nullstart - the position to start from, negative means count back from the end of the String by this many charactersnull if null String inputpublic static String substring(String str, int start, int end)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start/end n
 characters from the end of the String.
The returned substring starts with the character in the start
 position and ends before the end position. All position counting is
 zero-based -- i.e., to start at the beginning of the string use
 start = 0. Negative start and end positions can be used to
 specify offsets relative to the end of the String.
If start is not strictly to the left of end, ""
 is returned.
 StringUtils.substring(null, *, *)    = null
 StringUtils.substring("", * ,  *)    = "";
 StringUtils.substring("abc", 0, 2)   = "ab"
 StringUtils.substring("abc", 2, 0)   = ""
 StringUtils.substring("abc", 2, 4)   = "c"
 StringUtils.substring("abc", 4, 6)   = ""
 StringUtils.substring("abc", 2, 2)   = ""
 StringUtils.substring("abc", -2, -1) = "b"
 StringUtils.substring("abc", -4, 2)  = "ab"
 str - the String to get the substring from, may be nullstart - the position to start from, negative means count back from the end of the String by this many charactersend - the position to end at (exclusive), negative means count back from the end of the String by this many
             charactersnull if null String inputpublic static String upperCase(String str)
Converts a String to upper case as per String.toUpperCase().
A null input String returns null.
 StringUtils.upperCase(null)  = null
 StringUtils.upperCase("")    = ""
 StringUtils.upperCase("aBc") = "ABC"
 
 This uses "ENGLISH" as the locale.
str - the String to upper case, may be nullnull if null String inputpublic static String lowerCase(String str)
Converts a String to lower case as per String.toLowerCase().
A null input String returns null.
 StringUtils.lowerCase(null)  = null
 StringUtils.lowerCase("")    = ""
 StringUtils.lowerCase("aBc") = "abc"
 
 This uses "ENGLISH" as the locale.
str - the String to lower case, may be nullnull if null String inputpublic static String capitalize(String str)
Capitalizes a String changing the first character to title case as
 per Character.toTitleCase(int). No other characters are changed.
 StringUtils.capitalize(null)  = null
 StringUtils.capitalize("")    = ""
 StringUtils.capitalize("cat") = "Cat"
 StringUtils.capitalize("cAt") = "CAt"
 StringUtils.capitalize("'cat'") = "'cat'"
 str - the String to capitalize, may be nullnull if null String inputuncapitalize(String)public static String uncapitalize(String str)
Uncapitalizes a String, changing the first character to lower case as
 per Character.toLowerCase(int). No other characters are changed.
 StringUtils.uncapitalize(null)  = null
 StringUtils.uncapitalize("")    = ""
 StringUtils.uncapitalize("cat") = "cat"
 StringUtils.uncapitalize("Cat") = "cat"
 StringUtils.uncapitalize("CAT") = "cAT"
 str - the String to uncapitalize, may be nullnull if null String inputcapitalize(String)public static String fromBytes(byte[] bytes, Charset charset) throws UncheckedIOException
UncheckedIOException - with a CharacterCodingException as the cause if the bytes cannot be encoded using the
 provided charset.public static boolean startsWithIgnoreCase(String str, String prefix)
str - the string to be testedprefix - the prefixpublic static String replaceOnce(String text, String searchString, String replacement)
Replaces a String with another String inside a larger String, once.
A null reference passed to this method is a no-op.
 StringUtils.replaceOnce(null, *, *)        = null
 StringUtils.replaceOnce("", *, *)          = ""
 StringUtils.replaceOnce("any", null, *)    = "any"
 StringUtils.replaceOnce("any", *, null)    = "any"
 StringUtils.replaceOnce("any", "", *)      = "any"
 StringUtils.replaceOnce("aba", "a", null)  = "aba"
 StringUtils.replaceOnce("aba", "a", "")    = "ba"
 StringUtils.replaceOnce("aba", "a", "z")   = "zba"
 text - text to search and replace in, may be nullsearchString - the String to search for, may be nullreplacement - the String to replace with, may be nullnull if null String inputreplace(String text, String searchString, String replacement, int max)public static String replace(String text, String searchString, String replacement)
Replaces a String with another String inside a larger String,
 for the first max values of the search String,
 case sensitively/insensitively based on ignoreCase value.
A null reference passed to this method is a no-op.
 StringUtils.replace(null, *, *, *, false)         = null
 StringUtils.replace("", *, *, *, false)           = ""
 StringUtils.replace("any", null, *, *, false)     = "any"
 StringUtils.replace("any", *, null, *, false)     = "any"
 StringUtils.replace("any", "", *, *, false)       = "any"
 StringUtils.replace("any", *, *, 0, false)        = "any"
 StringUtils.replace("abaa", "a", null, -1, false) = "abaa"
 StringUtils.replace("abaa", "a", "", -1, false)   = "b"
 StringUtils.replace("abaa", "a", "z", 0, false)   = "abaa"
 StringUtils.replace("abaa", "A", "z", 1, false)   = "abaa"
 StringUtils.replace("abaa", "A", "z", 1, true)   = "zbaa"
 StringUtils.replace("abAa", "a", "z", 2, true)   = "zbza"
 StringUtils.replace("abAa", "a", "z", -1, true)  = "zbzz"
 text - text to search and replace in, may be nullsearchString - the String to search for (case insensitive), may be nullreplacement - the String to replace it with, may be nullnull if null String inputpublic static String replaceEach(String text, String[] searchList, String[] replacementList)
Replaces all occurrences of Strings within another String.
 A null reference passed to this method is a no-op, or if
 any "search string" or "string to replace" is null, that replace will be
 ignored. This will not repeat. For repeating replaces, call the
 overloaded method.
 
  StringUtils.replaceEach(null, *, *)        = null
  StringUtils.replaceEach("", *, *)          = ""
  StringUtils.replaceEach("aba", null, null) = "aba"
  StringUtils.replaceEach("aba", new String[0], null) = "aba"
  StringUtils.replaceEach("aba", null, new String[0]) = "aba"
  StringUtils.replaceEach("aba", new String[]{"a"}, null)  = "aba"
  StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""})  = "b"
  StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"})  = "aba"
  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"})  = "wcte"
  (example of how it does not repeat)
  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"})  = "dcte"
 text - text to search and replace in, no-op if nullsearchList - the Strings to search for, no-op if nullreplacementList - the Strings to replace them with, no-op if nullnull if
         null String inputIllegalArgumentException - if the lengths of the arrays are not the same (null is ok,
             and/or size 0)public static String replacePrefixIgnoreCase(String str, String prefix, String replacement)
The unmatched part is unchanged.
str - the string to replaceprefix - the prefix to findreplacement - the replacementpublic static Character findFirstOccurrence(String s, char... charsToMatch)
s - The string to search.charsToMatch - A list of characters to search the string for.public static boolean safeStringToBoolean(String value)
Boolean.parseBoolean(String)). If a customer
 specifies a boolean value it should be "true" or "false" (case insensitive) or an exception will be thrown.public static String repeat(String value, int count)
count times.
 If this string is empty or count is zero then the empty string is returned.
 Logical clone of JDK11's String#repeat(int).
value - the string to repeatcount - number of times to repeatcount times or the empty string if this string is empty or count
 is zeroIllegalArgumentException - if the count is negative.Copyright © 2023. All rights reserved.