Class StringTool

java.lang.Object
com.xxl.tool.core.StringTool

public class StringTool extends Object
string tool
Author:
xuxueli 2020-04-16 (some references to other libraries)
  • Field Details

  • Constructor Details

    • StringTool

      public StringTool()
  • Method Details

    • isEmpty

      public static boolean isEmpty(String str)
      is empty
       StringUtils.isEmpty(null)      = true
       StringUtils.isEmpty("")        = true
       StringUtils.isEmpty(" ")       = false
       StringUtils.isEmpty("bob")     = false
       StringUtils.isEmpty("  bob  ") = false
       
      Parameters:
      str -
      Returns:
    • isNotEmpty

      public static boolean isNotEmpty(String str)
      is not empty
       StringUtils.isNotEmpty(null)      = false
       StringUtils.isNotEmpty("")        = false
       StringUtils.isNotEmpty(" ")       = true
       StringUtils.isNotEmpty("bob")     = true
       StringUtils.isNotEmpty("  bob  ") = true
       
      Parameters:
      str -
      Returns:
    • isBlank

      public static boolean isBlank(String str)
      is blank
       StringUtils.isBlank(null)      = true
       StringUtils.isBlank("")        = true
       StringUtils.isBlank(" ")       = true
       StringUtils.isBlank("bob")     = false
       StringUtils.isBlank("  bob  ") = false
       
      Parameters:
      str -
      Returns:
    • isNotBlank

      public static boolean isNotBlank(String str)
      is not blank
       StringUtils.isNotBlank(null)      = false
       StringUtils.isNotBlank("")        = false
       StringUtils.isNotBlank(" ")       = false
       StringUtils.isNotBlank("bob")     = true
       StringUtils.isNotBlank("  bob  ") = true
       
      Parameters:
      str -
      Returns:
    • trim

      public static String trim(String str)
      trim
       StringUtils.trim(null)          = null
       StringUtils.trim("")            = ""
       StringUtils.trim("     ")       = ""
       StringUtils.trim("abc")         = "abc"
       StringUtils.trim("    abc    ") = "abc"
       
      Parameters:
      str -
      Returns:
    • trimToNull

      public static String trimToNull(String str)
      trimToNull
       StringUtils.trimToNull(null)          = null
       StringUtils.trimToNull("")            = null
       StringUtils.trimToNull("     ")       = null
       StringUtils.trimToNull("abc")         = "abc"
       StringUtils.trimToNull("    abc    ") = "abc"
       
      Parameters:
      str -
      Returns:
    • trimToEmpty

      public static String trimToEmpty(String str)
      trimToEmpty
       StringUtils.trimToEmpty(null)          = ""
       StringUtils.trimToEmpty("")            = ""
       StringUtils.trimToEmpty("     ")       = ""
       StringUtils.trimToEmpty("abc")         = "abc"
       StringUtils.trimToEmpty("    abc    ") = "abc"
       
      Parameters:
      str -
      Returns:
    • isNumeric

      public static boolean isNumeric(String str)
      isNumeric

      Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.

      null will return false. An empty String (length()=0) will return true.

       StringUtils.isNumeric(null)   = false
       StringUtils.isNumeric("")     = false
       StringUtils.isNumeric("  ")   = false
       StringUtils.isNumeric("123")  = true
       StringUtils.isNumeric("12 3") = false
       StringUtils.isNumeric("ab2c") = false
       StringUtils.isNumeric("12-3") = false
       StringUtils.isNumeric("12.3") = false
       
      Parameters:
      str - the String to check, may be null
      Returns:
      true if only contains digits, and is non-null
    • countMatches

      public static int countMatches(String str, String sub)
      Count matches

      Counts how many times the substring appears in the larger string.

      A null or empty ("") String input returns 0.

       StringUtils.countMatches(null, *)       = 0
       StringUtils.countMatches("", *)         = 0
       StringUtils.countMatches("abba", null)  = 0
       StringUtils.countMatches("abba", "")    = 0
       StringUtils.countMatches("abba", "a")   = 2
       StringUtils.countMatches("abba", "ab")  = 1
       StringUtils.countMatches("abba", "xxx") = 0
       
      Parameters:
      str - the CharSequence to check, may be null
      sub - the substring to count, may be null
      Returns:
      the number of occurrences, 0 if either CharSequence is null
    • upperCaseFirst

      public static String upperCaseFirst(String text)
      upperCase first letter
            StringUtils.upperCaseFirst(null, *)     = null
            StringUtils.upperCaseFirst("", *)       = ""
            StringUtils.countMatches("abc", *)      = "Abc"
       
      Parameters:
      text -
      Returns:
    • lowerCaseFirst

      public static String lowerCaseFirst(String text)
      lowerCase first letter
            StringUtils.lowerCaseFirst(null, *)     = null
            StringUtils.lowerCaseFirst("", *)       = ""
            StringUtils.lowerCaseFirst("ABC", *)    = "aBC"
       
      Parameters:
      text -
      Returns:
    • underlineToCamelCase

      public static String underlineToCamelCase(String underscoreText)
      convert underscores to hump format
            StringUtils.lowerCaseFirst(null, *)             = null
            StringUtils.lowerCaseFirst("", *)               = ""
            StringUtils.lowerCaseFirst("aaa_bbb", *)        = "aaaBbb"
       
      Parameters:
      underscoreText -
      Returns:
    • substring

      public static String substring(String str, int start)
      substring from start position
            StringTool.substring(null, *)   = null
            StringTool.substring("", *)     = ""
            StringTool.substring("abc", 0)  = "abc"
            StringTool.substring("abc", 2)  = "c"
            StringTool.substring("abc", 4)  = ""
            StringTool.substring("abc", -2) = "abc"
       
      Parameters:
      str - the String to get the substring from, may be null
      start - the position to start from, negative means
    • substring

      public static String substring(String str, int start, int end)
      substring from start position to end position
            StringTool.substring(null, *, *)    = null
            StringTool.substring("", * ,  *)    = "";
            StringTool.substring("abc", 1, 2)   = "b"
            StringTool.substring("abc", -1, 2)   = "ab"
            StringTool.substring("abc", 1, 0)   = ""
            StringTool.substring("abc", 1, 5)   = "bc"
            StringTool.substring("abc", 2, 1)   = ""
       
      Parameters:
      str - the String to get the substring from, may be null
      start - the position to start from, negative means
      end - the position to end at (exclusive), negative means
    • split

      public static List<String> split(String str, String separator)
      split str 2 array, with separator
           StringTool.split("a,b,c", ",")     = ["a","b","c"]
           StringTool.split("a,b,", ",")      = ["a","b"]
           StringTool.split("a, ,c", ",")     = ["a","c"]
       
      Parameters:
      str - string to split
      separator - separator to use for separating elements
      Returns:
    • split

      public static List<String> split(String str, String separator, boolean trimTokens, boolean ignoreBlackTokens)
      split str 2 array, with separator
           StringTool.split("a,b,c", ",")       = ["a","b","c"]
           StringTool.split("a, b ,c", ",")     = ["a","b","c"]
           StringTool.split("a,,c", ",")        = ["a","c"]
       
      Parameters:
      str - string to split
      separator - separator to use for separating elements
      trimTokens - true if the tokens should be trimmed
      ignoreBlackTokens - true if empty tokens should be removed from the result
      Returns:
    • join

      public static String join(List<String> list, String separator)
      join array to string
           StringTool.join(["a","b","c"], ",")     = "a,b,c"
           StringTool.join(["a","b"," c "], ",")   = "a,b,c"
           StringTool.join(["a","b",""], ",")      = "a,b"
           StringTool.join(["a",null,"c"], ",")    = "a,c"
       
      Parameters:
      list - list to join
      separator - separator to use between elements
      Returns:
    • join

      public static String join(List<String> list, String separator, boolean trimTokens, boolean ignoreBlackTokens)
      join array to string
      Parameters:
      list - list to join
      separator - separator to use between elements
      trimTokens - true if the tokens should be trimmed
      ignoreBlackTokens - true if empty tokens should be ignored
      Returns:
    • format

      public static String format(String template, Object... params)
      format string
           StringTool.format("hello,{0}!", "world"));                                    = hello,world!
           StringTool.format("hello,{0}!", null));                                       = hello,{0}!
           StringTool.format("hello,{0}!"));                                             = hello,{0}!
           StringTool.format("hello,{0}!", "world", "world"));                           = hello,world!
           StringTool.format("Hello {0}, welcome {1}!"));                                = Hello {0}, welcome {1}!
           StringTool.format("Hello {0}, welcome {1}!",null));                           = Hello {0}, welcome {1}!
           StringTool.format("Hello {0}, welcome {1}!",null, null));                     = Hello null, welcome null!
           StringTool.format("Hello {0}, welcome {1}!", "Alice"));                       = Hello Alice, welcome {1}!
           StringTool.format("Hello {0}, welcome {1}!", "Alice", "Jack"));               = Hello Alice, welcome Jack!
           StringTool.format("Hello {0}, welcome {1}!", "Alice", "Jack", "Lucy"));       = Hello Alice, welcome Jack!
           StringTool.format("Hello {0}, you have {1} messages", "Alice", 5));           = Hello Alice, you have 5 messages
           StringTool.format("{1} messages for {0}", "Alice", 5));                       = 5 messages for Alice
           StringTool.format("Hello {0}, welcome {0}!", "Alice"));                       = Hello Alice, welcome Alice!
           StringTool.format("Balance: {0,number}", 1234.56));                           = Balance: 1,234.56
           StringTool.format("Price: {0,number,currency}", 1234.56));                    = Price: ¥1,234.56
           StringTool.format("Success rate: {0,number,percent}", 0.85));                 = Success rate: 85%
           StringTool.format("Account: {0,number,#,##0.00}", 1234.5));                   = Account: 1,234.50
       
      Parameters:
      template - template string
      params - string array
      Returns:
    • formatWithMap

      public static String formatWithMap(String template, Map<String,Object> params)
      format string with map
           StringTool.formatWithMap("{name} is {age} years old", MapTool.newMap("name", "jack", "age", 18))        = jack is 18 years old
           StringTool.formatWithMap("{name} is {age} years old", null)                                             = {name} is {age} years old
           StringTool.formatWithMap("{name} is {age} years old", MapTool.newMap("name", "jack"))                   = jack is {age} years old
           StringTool.formatWithMap("{name} is {age} years old", MapTool.newMap("name", "jack", "age", null))      = jack is {age} years old
       
      Parameters:
      template - template string
      params - parameter map
      Returns:
    • replace

      public static String replace(String inString, String oldPattern, String newPattern)
      replace string
           StringTool.replace("hello jack, how are you", "jack", "lucy"));              = hello lucy, how are you
           StringTool.replace("hello jack, how are you, jack", "jack", "lucy"));        = hello lucy, how are you, lucy
           StringTool.replace("", "jack", "lucy"));                                     =
           StringTool.replace(null, "jack", "lucy"));                                   = null
           StringTool.replace("hello jack, how are you", null, "jack"));                = hello jack, how are you
           StringTool.replace("hello jack, how are you", "", "jack"));                  = hello jack, how are you
           StringTool.replace("hello jack, how are you", " ", "-"));                    = hello-jack,-how-are-you
           StringTool.replace("hello jack, how are you", "jack", null));                = hello jack, how are you
           StringTool.replace("hello jack, how are you", "jack", ""));                  = hello , how are you
           StringTool.replace("hello jack, how are you", "jack", " "));                 = hello  , how are you
       
      Parameters:
      inString - input string
      oldPattern - old pattern, empty string will not be replaced
      newPattern - new pattern, null string will not to replace
      Returns:
    • removePrefix

      public static String removePrefix(String str, String prefix)
      remove prefix
           StringTool.removePrefix("hello,world", "hello")                         =  ,world
           StringTool.removePrefix("hello,world", "world")                         =  hello,world
           StringTool.removePrefix("hello,world", "hello,world")                   =
           StringTool.removePrefix("hello,world", "")                              =  hello,world
           StringTool.removePrefix("hello,world", null)                            =  hello,world
           StringTool.removePrefix("", "world")                                    =
           StringTool.removePrefix(null, "world")                                  =  null
       
      Parameters:
      str - the string to remove prefix
      prefix - prefix to remove
    • removeSuffix

      public static String removeSuffix(String str, String suffix)
      remove suffix
           StringTool.removeSuffix("hello,world", "hello")                          hello,world
           StringTool.removeSuffix("hello,world", "world")                          hello,
           StringTool.removeSuffix("hello,world", "hello,world"))
           StringTool.removeSuffix("hello,world", "")                               hello,world
           StringTool.removeSuffix("hello,world", null)                             hello,world
           StringTool.removeSuffix("", "world")
           StringTool.removeSuffix(null, "world")                                   null
       
      Parameters:
      str - the string to remove suffix
      suffix - suffix to remove
      Returns:
    • equals

      public static boolean equals(String str1, String str2)
      string equals
           StringTool.equals("hello", "hello")                   = true
           StringTool.equals("hello", "world")                   = false
           StringTool.equals(null, null)                         = true
           StringTool.equals(null, "world")                      = false
           StringTool.equals("hello", null)                      = false
       
      Parameters:
      str1 - the first string to compare
      str2 - the second string to compare
      Returns:
      true if equals