类 StringUtils

java.lang.Object
com.alibaba.ageiport.common.utils.StringUtils

public class StringUtils extends Object
StringUtils
作者:
lingyi
  • 字段详细资料

  • 构造器详细资料

    • StringUtils

      public StringUtils()
  • 方法详细资料

    • isNotEmpty

      public static boolean isNotEmpty(CharSequence str)
      is not empty string.
      参数:
      str - source string.
      返回:
      is not empty.
    • isEmpty

      public static boolean isEmpty(CharSequence str)
      is empty string.
      参数:
      str - source string.
      返回:
      is empty.
    • isNotBlank

      public static boolean isNotBlank(CharSequence cs)
    • isBlank

      public static boolean isBlank(CharSequence cs)
    • format

      public static String format(String template, Object... params)
      格式化文本, {} 表示占位符
      此方法只是简单将占位符 {} 按照顺序替换为参数
      如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可
      例:
      通常使用:format("this is {} for {}", "a", "b") =》 this is a for b
      转义{}: format("this is \\{} for {}", "a", "b") =》 this is \{} for a
      转义\: format("this is \\\\{} for {}", "a", "b") =》 this is \a for b
      参数:
      template - 文本模板,被替换的部分用 {} 表示
      params - 参数值
      返回:
      格式化后的文本
    • subPre

      public static String subPre(CharSequence string, int toIndex)
      切割指定位置之前部分的字符串
      参数:
      string - 字符串
      toIndex - 切割到的位置(不包括)
      返回:
      切割后的剩余的前半部分字符串
    • sub

      public static String sub(CharSequence str, int fromIndex, int toIndex)
      改进JDK subString
      index从0开始计算,最后一个字符为-1
      如果from和to位置一样,返回 ""
      如果from或to为负数,则按照length从后向前数位置,如果绝对值大于字符串长度,则from归到0,to归到length
      如果经过修正的index中from大于to,则互换from和to example:
      abcdefgh 2 3 =》 c
      abcdefgh 2 -3 =》 cde
      参数:
      str - String
      fromIndex - 开始的index(包括)
      toIndex - 结束的index(不包括)
      返回:
      字串
    • str

      public static String str(CharSequence cs)
      CharSequence 转为字符串,null安全
      参数:
      cs - CharSequence
      返回:
      字符串
    • equals

      public static boolean equals(CharSequence str1, CharSequence str2, boolean ignoreCase)
      比较两个字符串是否相等。
      参数:
      str1 - 要比较的字符串1
      str2 - 要比较的字符串2
      ignoreCase - 是否忽略大小写
      返回:
      如果两个字符串相同,或者都是null,则返回true
    • contains

      public static boolean contains(CharSequence str1, CharSequence str2)
      第一个字符串是否包含第二个字符串
      参数:
      str1 - 字符串1
      str2 - 字符串2
    • toString

      public static String toString(Throwable e)
      参数:
      e -
      返回:
      string
    • camelToSplitName

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

      public static String trim(CharSequence str)
      除去字符串头尾部的空白,如果字符串是null,依然返回null

      注意,和String.trim不同,此方法使用NumberUtil.isBlankChar 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。

       trim(null)          = null
       trim("")            = ""
       trim("     ")       = ""
       trim("abc")         = "abc"
       trim("    abc    ") = "abc"
       
      参数:
      str - 要处理的字符串
      返回:
      除去头尾空白的字符串,如果原字串为null,则返回null
    • trim

      public static String trim(CharSequence str, int mode)
      除去字符串头尾部的空白符,如果字符串是null,依然返回null
      参数:
      str - 要处理的字符串
      mode - -1表示trimStart,0表示trim全部, 1表示trimEnd
      返回:
      除去指定字符后的的字符串,如果原字串为null,则返回null
    • trimToNull

      public 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. To strip whitespace use stripToNull(String).

       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 null
      返回:
      the trimmed String, null if only chars <= 32, empty or null String input
      从以下版本开始:
      2.0
    • trimToEmpty

      public 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. To strip whitespace use stripToEmpty(String).

       StringUtils.trimToEmpty(null)          = ""
       StringUtils.trimToEmpty("")            = ""
       StringUtils.trimToEmpty("     ")       = ""
       StringUtils.trimToEmpty("abc")         = "abc"
       StringUtils.trimToEmpty("    abc    ") = "abc"
       
      参数:
      str - the String to be trimmed, may be null
      返回:
      the trimmed String, or an empty String if null input
      从以下版本开始:
      2.0
    • strip

      public static String strip(String str)

      Strips whitespace from the start and end of a String.

      Whitespace is defined by Character.isWhitespace(char).

      A null input String returns null.

       StringUtils.strip(null)     = null
       StringUtils.strip("")       = ""
       StringUtils.strip("   ")    = ""
       StringUtils.strip("abc")    = "abc"
       StringUtils.strip("  abc")  = "abc"
       StringUtils.strip("abc  ")  = "abc"
       StringUtils.strip(" abc ")  = "abc"
       StringUtils.strip(" ab c ") = "ab c"
       
      参数:
      str - the String to remove whitespace from, may be null
      返回:
      the stripped String, null if null String input
    • stripToNull

      public static String stripToNull(String str)

      Strips whitespace from the start and end of a String returning null if the String is empty ("") after the strip.

      This is similar to trimToNull(String) but removes whitespace. Whitespace is defined by Character.isWhitespace(char).

       StringUtils.stripToNull(null)     = null
       StringUtils.stripToNull("")       = null
       StringUtils.stripToNull("   ")    = null
       StringUtils.stripToNull("abc")    = "abc"
       StringUtils.stripToNull("  abc")  = "abc"
       StringUtils.stripToNull("abc  ")  = "abc"
       StringUtils.stripToNull(" abc ")  = "abc"
       StringUtils.stripToNull(" ab c ") = "ab c"
       
      参数:
      str - the String to be stripped, may be null
      返回:
      the stripped String, null if whitespace, empty or null String input
      从以下版本开始:
      2.0
    • stripToEmpty

      public static String stripToEmpty(String str)

      Strips whitespace from the start and end of a String returning an empty String if null input.

      This is similar to trimToEmpty(String) but removes whitespace. Whitespace is defined by Character.isWhitespace(char).

       StringUtils.stripToEmpty(null)     = ""
       StringUtils.stripToEmpty("")       = ""
       StringUtils.stripToEmpty("   ")    = ""
       StringUtils.stripToEmpty("abc")    = "abc"
       StringUtils.stripToEmpty("  abc")  = "abc"
       StringUtils.stripToEmpty("abc  ")  = "abc"
       StringUtils.stripToEmpty(" abc ")  = "abc"
       StringUtils.stripToEmpty(" ab c ") = "ab c"
       
      参数:
      str - the String to be stripped, may be null
      返回:
      the trimmed String, or an empty String if null input
      从以下版本开始:
      2.0
    • strip

      public static String strip(String str, String stripChars)

      Strips any of a set of characters from the start and end of a String. This is similar to String.trim() but allows the characters to be stripped to be controlled.

      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). Alternatively use strip(String).

       StringUtils.strip(null, *)          = null
       StringUtils.strip("", *)            = ""
       StringUtils.strip("abc", null)      = "abc"
       StringUtils.strip("  abc", null)    = "abc"
       StringUtils.strip("abc  ", null)    = "abc"
       StringUtils.strip(" abc ", null)    = "abc"
       StringUtils.strip("  abcyx", "xyz") = "  abc"
       
      参数:
      str - the String to remove characters from, may be null
      stripChars - the characters to remove, null treated as whitespace
      返回:
      the stripped String, null if null String input
    • stripStart

      public static String stripStart(String str, String stripChars)

      Strips any of a set of characters from the start 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.stripStart(null, *)          = null
       StringUtils.stripStart("", *)            = ""
       StringUtils.stripStart("abc", "")        = "abc"
       StringUtils.stripStart("abc", null)      = "abc"
       StringUtils.stripStart("  abc", null)    = "abc"
       StringUtils.stripStart("abc  ", null)    = "abc  "
       StringUtils.stripStart(" abc ", null)    = "abc "
       StringUtils.stripStart("yxabc  ", "xyz") = "abc  "
       
      参数:
      str - the String to remove characters from, may be null
      stripChars - the characters to remove, null treated as whitespace
      返回:
      the stripped String, null if null String input
    • 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"
       
      参数:
      str - the String to remove characters from, may be null
      stripChars - the set of characters to remove, null treated as whitespace
      返回:
      the stripped String, null if null String input
    • stripAll

      public static String[] stripAll(String[] strs)

      Strips whitespace from the start and end of every String in an array. Whitespace is defined by Character.isWhitespace(char).

      A new array is returned each time, except for length zero. A null array will return null. An empty array will return itself. A null array entry will be ignored.

       StringUtils.stripAll(null)             = null
       StringUtils.stripAll([])               = []
       StringUtils.stripAll(["abc", "  abc"]) = ["abc", "abc"]
       StringUtils.stripAll(["abc  ", null])  = ["abc", null]
       
      参数:
      strs - the array to remove whitespace from, may be null
      返回:
      the stripped Strings, null if null array input
    • stripAll

      public static String[] stripAll(String[] strs, String stripChars)

      Strips any of a set of characters from the start and end of every String in an array.

      Whitespace is defined by Character.isWhitespace(char).

      A new array is returned each time, except for length zero. A null array will return null. An empty array will return itself. A null array entry will be ignored. A null stripChars will strip whitespace as defined by Character.isWhitespace(char).

       StringUtils.stripAll(null, *)                = null
       StringUtils.stripAll([], *)                  = []
       StringUtils.stripAll(["abc", "  abc"], null) = ["abc", "abc"]
       StringUtils.stripAll(["abc  ", null], null)  = ["abc", null]
       StringUtils.stripAll(["abc  ", null], "yz")  = ["abc  ", null]
       StringUtils.stripAll(["yabcz", null], "yz")  = ["abc", null]
       
      参数:
      strs - the array to remove characters from, may be null
      stripChars - the characters to remove, null treated as whitespace
      返回:
      the stripped Strings, null if null array input
    • subBefore

      public static String subBefore(CharSequence string, char separator, boolean isLastSeparator)
      截取分隔字符串之前的字符串,不包括分隔字符串
      如果给定的字符串为空串(null或"")或者分隔字符串为null,返回原字符串
      如果分隔字符串未找到,返回原字符串,举例如下:
       StrUtil.subBefore(null, *)      = null
       StrUtil.subBefore("", *)        = ""
       StrUtil.subBefore("abc", 'a')   = ""
       StrUtil.subBefore("abcba", 'b') = "a"
       StrUtil.subBefore("abc", 'c')   = "ab"
       StrUtil.subBefore("abc", 'd')   = "abc"
       
      参数:
      string - 被查找的字符串
      separator - 分隔字符串(不包括)
      isLastSeparator - 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个
      返回:
      切割后的字符串
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(CharSequence str, CharSequence suffix)
    • startWithIgnoreCase

      public static boolean startWithIgnoreCase(CharSequence str, CharSequence prefix)
      是否以指定字符串开头,忽略大小写
      参数:
      str - 被监测字符串
      prefix - 开头字符串
      返回:
      是否以指定字符串开头
    • startWith

      public static boolean startWith(CharSequence str, CharSequence prefix, boolean isIgnoreCase)
      是否以指定字符串开头
      如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false
      参数:
      str - 被监测字符串
      prefix - 开头字符串
      isIgnoreCase - 是否忽略大小写
      返回:
      是否以指定字符串开头
    • removeAll

      public static String removeAll(CharSequence str, char... chars)
      去除字符串中指定的多个字符,如有多个则全部去除
      参数:
      str - 字符串
      chars - 字符列表
      返回:
      去除后的字符
    • padStart

      public static String padStart(String string, int minLength, char padChar)
    • padEnd

      public static String padEnd(String string, int minLength, char padChar)
    • padMiddle

      public static String padMiddle(int maxLength, String start, String end, char padChar)
    • lenientFormat

      public static String lenientFormat(String template, Object... args)