Class StringUtils

java.lang.Object
org.springframework.util.StringUtils

public abstract class StringUtils extends Object
Miscellaneous String utility methods.

Mainly for internal use within the framework; consider Apache's Commons Lang for a more comprehensive suite of String utilities.

This class delivers some simple functionality that should really be provided by the core Java String and StringBuilder classes. It also provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.

Since:
16 April 2001
Author:
Rod Johnson, Juergen Hoeller, Keith Donald, Rob Harrop, Rick Evans, Arjen Poutsma, Sam Brannen, Brian Clozel, Sebastien Deleuze
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • isEmpty

      @Deprecated public static boolean isEmpty(@Nullable Object str)
      Deprecated.
      Check whether the given object (possibly a String) is empty. This is effectively a shortcut for !hasLength(String).

      This method accepts any Object as an argument, comparing it to null and the empty String. As a consequence, this method will never return true for a non-null non-String object.

      The Object signature is useful for general attribute handling code that commonly deals with Strings but generally has to iterate over Objects since attributes may e.g. be primitive value objects as well.

      Note: If the object is typed to String upfront, prefer hasLength(String) or hasText(String) instead.

      Parameters:
      str - the candidate object (possibly a String)
      Since:
      3.2.1
    • hasLength

      public static boolean hasLength(@Nullable CharSequence str)
      Check that the given CharSequence is neither null nor of length 0.

      Note: this method returns true for a CharSequence that purely consists of whitespace.

       StringUtils.hasLength(null) = false
       StringUtils.hasLength("") = false
       StringUtils.hasLength(" ") = true
       StringUtils.hasLength("Hello") = true
       
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is not null and has length
      See Also:
    • hasLength

      public static boolean hasLength(@Nullable String str)
      Check that the given String is neither null nor of length 0.

      Note: this method returns true for a String that purely consists of whitespace.

      Parameters:
      str - the String to check (may be null)
      Returns:
      true if the String is not null and has length
      See Also:
    • hasText

      public static boolean hasText(@Nullable CharSequence str)
      Check whether the given CharSequence contains actual text.

      More specifically, this method returns true if the CharSequence is not null, its length is greater than 0, and it contains at least one non-whitespace character.

       StringUtils.hasText(null) = false
       StringUtils.hasText("") = false
       StringUtils.hasText(" ") = false
       StringUtils.hasText("12345") = true
       StringUtils.hasText(" 12345 ") = true
       
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is not null, its length is greater than 0, and it does not contain whitespace only
      See Also:
    • hasText

      public static boolean hasText(@Nullable String str)
      Check whether the given String contains actual text.

      More specifically, this method returns true if the String is not null, its length is greater than 0, and it contains at least one non-whitespace character.

      Parameters:
      str - the String to check (may be null)
      Returns:
      true if the String is not null, its length is greater than 0, and it does not contain whitespace only
      See Also:
    • containsWhitespace

      public static boolean containsWhitespace(@Nullable CharSequence str)
      Check whether the given CharSequence contains any whitespace characters.
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is not empty and contains at least 1 whitespace character
      See Also:
    • containsWhitespace

      public static boolean containsWhitespace(@Nullable String str)
      Check whether the given String contains any whitespace characters.
      Parameters:
      str - the String to check (may be null)
      Returns:
      true if the String is not empty and contains at least 1 whitespace character
      See Also:
    • trimWhitespace

      @Deprecated(since="6.0") public static String trimWhitespace(String str)
      Deprecated.
      since 6.0, in favor of String.strip()
      Trim leading and trailing whitespace from the given String.
      Parameters:
      str - the String to check
      Returns:
      the trimmed String
      See Also:
    • trimAllWhitespace

      public static CharSequence trimAllWhitespace(CharSequence str)
      Trim all whitespace from the given CharSequence: leading, trailing, and in between characters.
      Parameters:
      str - the CharSequence to check
      Returns:
      the trimmed CharSequence
      Since:
      5.3.22
      See Also:
    • trimAllWhitespace

      public static String trimAllWhitespace(String str)
      Trim all whitespace from the given String: leading, trailing, and in between characters.
      Parameters:
      str - the String to check
      Returns:
      the trimmed String
      See Also:
    • trimLeadingWhitespace

      @Deprecated(since="6.0") public static String trimLeadingWhitespace(String str)
      Deprecated.
      since 6.0, in favor of String.stripLeading()
      Trim leading whitespace from the given String.
      Parameters:
      str - the String to check
      Returns:
      the trimmed String
      See Also:
    • trimTrailingWhitespace

      @Deprecated(since="6.0") public static String trimTrailingWhitespace(String str)
      Deprecated.
      since 6.0, in favor of String.stripTrailing()
      Trim trailing whitespace from the given String.
      Parameters:
      str - the String to check
      Returns:
      the trimmed String
      See Also:
    • trimLeadingCharacter

      public static String trimLeadingCharacter(String str, char leadingCharacter)
      Trim all occurrences of the supplied leading character from the given String.
      Parameters:
      str - the String to check
      leadingCharacter - the leading character to be trimmed
      Returns:
      the trimmed String
    • trimTrailingCharacter

      public static String trimTrailingCharacter(String str, char trailingCharacter)
      Trim all occurrences of the supplied trailing character from the given String.
      Parameters:
      str - the String to check
      trailingCharacter - the trailing character to be trimmed
      Returns:
      the trimmed String
    • matchesCharacter

      public static boolean matchesCharacter(@Nullable String str, char singleCharacter)
      Test if the given String matches the given single character.
      Parameters:
      str - the String to check
      singleCharacter - the character to compare to
      Since:
      5.2.9
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(@Nullable String str, @Nullable String prefix)
      Test if the given String starts with the specified prefix, ignoring upper/lower case.
      Parameters:
      str - the String to check
      prefix - the prefix to look for
      See Also:
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(@Nullable String str, @Nullable String suffix)
      Test if the given String ends with the specified suffix, ignoring upper/lower case.
      Parameters:
      str - the String to check
      suffix - the suffix to look for
      See Also:
    • substringMatch

      public static boolean substringMatch(CharSequence str, int index, CharSequence substring)
      Test whether the given string matches the given substring at the given index.
      Parameters:
      str - the original string (or StringBuilder)
      index - the index in the original string to start matching against
      substring - the substring to match at the given index
    • countOccurrencesOf

      public static int countOccurrencesOf(String str, String sub)
      Count the occurrences of the substring sub in string str.
      Parameters:
      str - string to search in
      sub - string to search for
    • replace

      public static String replace(String inString, String oldPattern, @Nullable String newPattern)
      Replace all occurrences of a substring within a string with another string.
      Parameters:
      inString - String to examine
      oldPattern - String to replace
      newPattern - String to insert
      Returns:
      a String with the replacements
    • delete

      public static String delete(String inString, String pattern)
      Delete all occurrences of the given substring.
      Parameters:
      inString - the original String
      pattern - the pattern to delete all occurrences of
      Returns:
      the resulting String
    • deleteAny

      public static String deleteAny(String inString, @Nullable String charsToDelete)
      Delete any character in a given String.
      Parameters:
      inString - the original String
      charsToDelete - a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.
      Returns:
      the resulting String
    • quote

      @Nullable public static String quote(@Nullable String str)
      Quote the given String with single quotes.
      Parameters:
      str - the input String (e.g. "myString")
      Returns:
      the quoted String (e.g. "'myString'"), or null if the input was null
    • quoteIfString

      @Nullable public static Object quoteIfString(@Nullable Object obj)
      Turn the given Object into a String with single quotes if it is a String; keeping the Object as-is else.
      Parameters:
      obj - the input Object (e.g. "myString")
      Returns:
      the quoted String (e.g. "'myString'"), or the input object as-is if not a String
    • unqualify

      public static String unqualify(String qualifiedName)
      Unqualify a string qualified by a '.' dot character. For example, "this.name.is.qualified", returns "qualified".
      Parameters:
      qualifiedName - the qualified name
    • unqualify

      public static String unqualify(String qualifiedName, char separator)
      Unqualify a string qualified by a separator character. For example, "this:name:is:qualified" returns "qualified" if using a ':' separator.
      Parameters:
      qualifiedName - the qualified name
      separator - the separator
    • capitalize

      public static String capitalize(String str)
      Capitalize a String, changing the first letter to upper case as per Character.toUpperCase(char). No other letters are changed.
      Parameters:
      str - the String to capitalize
      Returns:
      the capitalized String
    • uncapitalize

      public static String uncapitalize(