Class StringUtils

java.lang.Object
org.apache.dubbo.common.utils.StringUtils

public final class StringUtils extends Object
StringUtils
  • Field Details

  • Method Details

    • length

      public static int length(CharSequence cs)
      Gets a CharSequence length or 0 if the CharSequence is null.
      Parameters:
      cs - a CharSequence or null
      Returns:
      CharSequence length or 0 if the CharSequence is null.
    • repeat

      public static String repeat(String str, int repeat)

      Repeat a String repeat times to form a new String.

       StringUtils.repeat(null, 2) = null
       StringUtils.repeat("", 0)   = ""
       StringUtils.repeat("", 2)   = ""
       StringUtils.repeat("a", 3)  = "aaa"
       StringUtils.repeat("ab", 2) = "abab"
       StringUtils.repeat("a", -2) = ""
       
      Parameters:
      str - the String to repeat, may be null
      repeat - number of times to repeat str, negative treated as zero
      Returns:
      a new String consisting of the original String repeated, null if null String input
    • repeat

      public static String repeat(String str, String separator, int repeat)

      Repeat a String repeat times to form a new String, with a String separator injected each time.

       StringUtils.repeat(null, null, 2) = null
       StringUtils.repeat(null, "x", 2)  = null
       StringUtils.repeat("", null, 0)   = ""
       StringUtils.repeat("", "", 2)     = ""
       StringUtils.repeat("", "x", 3)    = "xxx"
       StringUtils.repeat("?", ", ", 3)  = "?, ?, ?"
       
      Parameters:
      str - the String to repeat, may be null
      separator - the String to inject, may be null
      repeat - number of times to repeat str, negative treated as zero
      Returns:
      a new String consisting of the original String repeated, null if null String input
      Since:
      2.5
    • removeEnd

      public static String removeEnd(String str, String remove)

      Removes a substring only if it is at the end of a source string, otherwise returns the source string.

      A null source string will return null. An empty ("") source string will return the empty string. A null search string will return the source string.

       StringUtils.removeEnd(null, *)      = null
       StringUtils.removeEnd("", *)        = ""
       StringUtils.removeEnd(*, null)      = *
       StringUtils.removeEnd("www.domain.com", ".com.")  = "www.domain.com"
       StringUtils.removeEnd("www.domain.com", ".com")   = "www.domain"
       StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com"
       StringUtils.removeEnd("abc", "")    = "abc"
       
      Parameters:
      str - the source String to search, may be null
      remove - the String to search for and remove, may be null
      Returns:
      the substring with the string removed if found, null if null String input
    • repeat

      public static String repeat(char ch, int repeat)

      Returns padding using the specified delimiter repeated to a given length.

       StringUtils.repeat('e', 0)  = ""
       StringUtils.repeat('e', 3)  = "eee"
       StringUtils.repeat('e', -2) = ""
       

      Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a pair of chars to be represented. If you are needing to support full I18N of your applications consider using repeat(String, int) instead.

      Parameters:
      ch - character to repeat
      repeat - number of times to repeat char, negative treated as zero
      Returns:
      String with repeated character
      See Also:
    • stripEnd

      public static String stripEnd(String str, String stripChars)

      Strips any of a set of characters from the end of a String.

      A null input String returns null. An empty string ("") input returns the empty string.

      If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

       StringUtils.stripEnd(null, *)          = null
       StringUtils.stripEnd("", *)            = ""
       StringUtils.stripEnd("abc", "")        = "abc"
       StringUtils.stripEnd("abc", null)      = "abc"
       StringUtils.stripEnd("  abc", null)    = "  abc"
       StringUtils.stripEnd("abc  ", null)    = "abc"
       StringUtils.stripEnd(" abc ", null)    = " abc"
       StringUtils.stripEnd("  abcyx", "xyz") = "  abc"
       StringUtils.stripEnd("120.00", ".0")   = "12"
       
      Parameters:
      str - the String to remove characters from, may be null
      stripChars - the set of characters to remove, null treated as whitespace
      Returns:
      the stripped String, null if null String input
    • replace

      public static String replace(String text, String searchString, String replacement)

      Replaces all occurrences of a String within another String.

      A null reference passed to this method is a no-op.

       StringUtils.replace(null, *, *)        = null
       StringUtils.replace("", *, *)          = ""
       StringUtils.replace("any", null, *)    = "any"
       StringUtils.replace("any", *, null)    = "any"
       StringUtils.replace("any", "", *)      = "any"
       StringUtils.replace("aba", "a", null)  = "aba"
       StringUtils.replace("aba", "a", "")    = "b"
       StringUtils.replace("aba", "a", "z")   = "zbz"
       
      Parameters:
      text - text to search and replace in, may be null
      searchString - the String to search for, may be null
      replacement - the String to replace it with, may be null
      Returns:
      the text with any replacements processed, null if null String input
      See Also:
    • replace

      public static String replace(String text, String searchString, String replacement, int max)

      Replaces a String with another String inside a larger String, for the first max values of the search String.

      A null reference passed to this method is a no-op.

       StringUtils.replace(null, *, *, *)         = null
       StringUtils.replace("", *, *, *)           = ""
       StringUtils.replace("any", null, *, *)     = "any"
       StringUtils.replace("any", *, null, *)     = "any"
       StringUtils.replace("any", "", *, *)       = "any"
       StringUtils.replace("any", *, *, 0)        = "any"
       StringUtils.replace("abaa", "a", null, -1) = "abaa"
       StringUtils.replace("abaa", "a", "", -1)   = "b"
       StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
       StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
       StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
       StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"
       
      Parameters:
      text - text to search and replace in, may be null
      searchString - the String to search for, may be null
      replacement - the String to replace it with, may be null
      max - maximum number of values to replace, or -1 if no maximum
      Returns:
      the text with any replacements processed, null if null String input
    • isBlank

      public static boolean isBlank(CharSequence cs)
    • isNotBlank

      public static boolean isNotBlank(CharSequence cs)
      is not blank string.
      Parameters:
      cs - source string.
      Returns:
      is not blank.
    • hasText

      public static boolean hasText(CharSequence cs)
      Check the cs String whether contains non whitespace characters.
      Parameters:
      cs -
      Returns:
    • isEmpty

      public static boolean isEmpty(String str)
      is empty string.
      Parameters:
      str - source string.
      Returns:
      is empty.
    • isNoneEmpty

      public static boolean isNoneEmpty(String... ss)

      Checks if the strings contain empty or null elements.

       StringUtils.isNoneEmpty(null)            = false
       StringUtils.isNoneEmpty("")              = false
       StringUtils.isNoneEmpty(" ")             = true
       StringUtils.isNoneEmpty("abc")           = true
       StringUtils.isNoneEmpty("abc", "def")    = true
       StringUtils.isNoneEmpty("abc", null)     = false
       StringUtils.isNoneEmpty("abc", "")       = false
       StringUtils.isNoneEmpty("abc", " ")      = true
       
      Parameters:
      ss - the strings to check
      Returns:
      true if all strings are not empty or null
    • isAnyEmpty

      public static boolean isAnyEmpty(String... ss)

      Checks if the strings contain at least on empty or null element.

       StringUtils.isAnyEmpty(null)            = true
       StringUtils.isAnyEmpty("")              = true
       StringUtils.isAnyEmpty(" ")             = false
       StringUtils.isAnyEmpty("abc")           = false
       StringUtils.isAnyEmpty("abc", "def")    = false
       StringUtils.isAnyEmpty("abc", null)     = true
       StringUtils.isAnyEmpty("abc", "")       = true
       StringUtils.isAnyEmpty("abc", " ")      = false
       
      Parameters:
      ss - the strings to check
      Returns:
      true if at least one in the strings is empty or null
    • isNotEmpty

      public static boolean isNotEmpty(String str)
      is not empty string.
      Parameters:
      str - source string.
      Returns:
      is not empty.
    • isEquals

      public static boolean isEquals(String s1, String s2)
      if s1 is null and s2 is null, then return true
      Parameters:
      s1 - str1
      s2 - str2
      Returns:
      equals
    • isNumber

      public static boolean isNumber(String str)
      is positive integer or zero string.
      Parameters:
      str - a string
      Returns:
      is positive integer or zero
    • parseInteger

      public static int parseInteger(String str)
      parse str to Integer(if str is not number or n invalid input: '<' 0, then return 0)
      Parameters:
      str - a number str
      Returns:
      positive integer or zero
    • parseLong

      public static long parseLong(String str)
      parse str to Long(if str is not number or n invalid input: '<' 0, then return 0)
      Parameters:
      str - a number str
      Returns:
      positive long or zero
    • isJavaIdentifier

      public static boolean isJavaIdentifier(String s)
      Returns true if s is a legal Java identifier.

      more info.

    • isContains

      public static boolean isContains(String values, String value)
    • isContains

      public static boolean isContains(String str, char ch)
    • isNotContains

      public static boolean isNotContains(String str, char ch)
    • isContains

      public static boolean isContains(String[] values, String value)
      Parameters:
      values -
      value -
      Returns:
      contains
    • isNumeric

      public static boolean isNumeric(String str, boolean allowDot)
    • toString

      public static String toString(Throwable e)
      Parameters:
      e -
      Returns:
      string
    • toString

      public static String toString(String msg, Throwable e)
      Parameters:
      msg -
      e -
      Returns:
      string
    • translate

      public static String translate(String src, String from, String to)
      translate.
      Parameters:
      src - source string.
      from - src char table.
      to - target char table.
      Returns:
      String.
    • split

      public static String[] split(String str, char ch)
      split.
      Parameters:
      ch - char.
      Returns:
      string array.
    • splitToList

      public static List<String> splitToList(String str, char ch)
      Splits String around matches of the given character.

      Note: Compare with split(String, char), this method reduce memory copy.

    • splitToSet

      public static Set<String> splitToSet(String value, char separatorChar)
      Split the specified value to be a Set
      Parameters:
      value - the content to be split
      separatorChar - a char to separate
      Returns:
      non-null read-only Set
      Since:
      2.7.8
    • splitToSet

      public static Set<String> splitToSet(String value, char separatorChar, boolean trimElements)
      Split the specified value to be a Set
      Parameters:
      value - the content to be split
      separatorChar - a char to separate
      trimElements - require to trim the elements or not
      Returns:
      non-null read-only Set
      Since:
      2.7.8
    • join

      public static String join(String[] array)
      join string.
      Parameters:
      array - String array.
      Returns:
      String.
    • join

      public static String join(String[] array, char split)
      join string like javascript.
      Parameters:
      array - String array.
      split - split
      Returns:
      String.
    • join

      public static String join(String[] array, String split)
      join string like javascript.
      Parameters:
      array - String array.
      split - split
      Returns:
      String.
    • join

      public static String join(Collection<String> coll, String split)
    • join

      public static String join(Object[] array, char delimiter, int startIndex, int endIndex)
    • getQueryStringValue

      public static String getQueryStringValue(String qs, String key)
    • parseQueryString

      public static Map<String,String> parseQueryString(String qs)
      parse query string to Parameters.
      Parameters:
      qs - query string.
      Returns:
      Parameters instance.
    • getServiceKey

      public static String getServiceKey(Map<String,String> ps)
    • toQueryString

      public static String toQueryString(Map<String,String> ps)
    • camelToSplitName

      public static String camelToSplitName(String camelName, String split)
    • snakeToSplitName

      public static String snakeToSplitName(String snakeName, String split)
      Convert snake_case or SNAKE_CASE to kebab-case.

      NOTE: Return itself if it's not a snake case.

      Parameters:
      snakeName -
      split -
      Returns:
    • convertToSplitName

      public static String convertToSplitName(String str, String split)
      Convert camelCase or snake_case/SNAKE_CASE to kebab-case
      Parameters:
      str -
      split -
      Returns:
    • toArgumentString

      public static String toArgumentString(Object[] args)
    • trim

      public static String trim(String str)
    • toURLKey

      public static String toURLKey(String key)
    • toOSStyleKey

      public static String toOSStyleKey(String key)
    • isAllUpperCase

      public static boolean isAllUpperCase(String str)
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(String str, String delimiter)
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)
    • arrayToDelimitedString

      public static String arrayToDelimitedString(Object[] arr, String delim)
    • deleteAny

      public static String deleteAny(String inString, String charsToDelete)
    • toStringArray

      public static String[] toStringArray(Collection<String> collection)
    • nullSafeToString

      public static String nullSafeToString(Object obj)
    • parseParameters

      public static Map<String,String> parseParameters(String rawParameters)
      Decode parameters string to map
      Parameters:
      rawParameters - format like '[{a:b},{c:d}]'
      Returns:
    • encodeParameters

      public static String encodeParameters(Map<String,String> params)
      Encode parameters map to string, like '[{a:b},{c:d}]'
      Parameters:
      params -
      Returns:
    • decodeHexNibble

      public static int decodeHexNibble(char c)
    • decodeHexByte

      public static byte decodeHexByte(CharSequence s, int pos)
      Decode a 2-digit hex byte from within a string.
    • toCommaDelimitedString

      public static String toCommaDelimitedString(String one, String... others)
      Creates a comma-delimited string from one or more string values.
      Parameters:
      one - the first string value
      others - additional string values
      Returns:
      the combined string, or null if the first value is null
      Since:
      2.7.8
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(String str, String prefix)
      Test str whether starts with the prefix ignore case.
    • defaultIf

      public static String defaultIf(String str, String defaultStr)
      Returns the default string if the input string is empty, otherwise returns the input string itself
    • substring

      public static String substring(String str, int start, int end)
      Gets a substring from the specified String avoiding exceptions. If end index is not found, returns substring from start to the end
    • substringBefore

      public static String substringBefore(String str, int separator)
      Gets the substring before the first occurrence of a separator.

      If nothing is found, returns the original string

    • substringAfter

      public static String substringAfter(String str, int separator)
      Gets the substring after the first occurrence of a separator.

      If nothing is found, the empty string is returned.

    • substringBeforeLast

      public static String substringBeforeLast(String str, int separator)
      Gets the substring before the last occurrence of a separator.

      If nothing is found, returns the original string

    • substringAfterLast

      public static String substringAfterLast(String str, int separator)
      Gets the substring after the last occurrence of a separator.

      If nothing is found, the empty string is returned.

    • tokenize

      public static String[] tokenize(String str, char... separators)
      Tokenize the given String into a String array. Trims tokens and omits empty tokens.
    • tokenizeToList

      public static List<String> tokenizeToList(String str, char... separators)
      Splits a string into a list of tokens using specified separators, trimming whitespace and ignoring empty tokens. Uses comma as default separator if none provided.
    • toBoolean

      public static Boolean toBoolean(String value)
      Converts string to Boolean based on common boolean representations. Supports values like 'true'/'false', 'yes'/'no', 'on'/'off', '1'/'0', etc. Returns null if the input cannot be parsed.
    • toBoolean

      public static boolean toBoolean(String value, boolean defaultValue)