类 StrUtil

java.lang.Object
org.tio.utils.hutool.StrUtil

public class StrUtil extends Object
  • 字段详细资料

  • 构造器详细资料

    • StrUtil

      public StrUtil()
  • 方法详细资料

    • int2Str

      public static String int2Str(int data)
      用缓存将int转成str
      参数:
      data -
      返回:
    • trim

      public static String trim(String value)
      去除字符串两边空白符,传入null也返回null
      参数:
      value - 值
      返回:
      去除空白符的值
    • isEmpty

      public static boolean isEmpty(CharSequence str)
      字符串是否为空,空的定义如下:
      1、为null
      2、为""
      参数:
      str - 被检测的字符串
      返回:
      是否为空
    • isNotBlank

      public static boolean isNotBlank(CharSequence str)
      字符串是否为非空白 空白的定义如下:
      1、不为null
      2、不为不可见字符(如空格)
      3、不为""
      参数:
      str - 被检测的字符串
      返回:
      是否为非空
    • isBlank

      public static boolean isBlank(CharSequence str)
      字符串是否为空白 空白的定义如下:
      1、为null
      2、为不可见字符(如空格)
      3、""
      参数:
      str - 被检测的字符串
      返回:
      是否为空
    • arrayToString

      public static String arrayToString(Object obj)
      数组或集合转String
      参数:
      obj - 集合或数组对象
      返回:
      数组字符串,与集合转字符串格式相同
    • 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 - 是否忽略大小写
      返回:
      是否以指定字符串开头
    • equals

      public static boolean equals(CharSequence str1, CharSequence str2)
      比较两个字符串(大小写敏感)。
       equals(null, null)   = true
       equals(null, "abc")  = false
       equals("abc", null)  = false
       equals("abc", "abc") = true
       equals("abc", "ABC") = false
       
      参数:
      str1 - 要比较的字符串1
      str2 - 要比较的字符串2
      返回:
      如果两个字符串相同,或者都是null,则返回true
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2)
      比较两个字符串(大小写不敏感)。
       equalsIgnoreCase(null, null)   = true
       equalsIgnoreCase(null, "abc")  = false
       equalsIgnoreCase("abc", null)  = false
       equalsIgnoreCase("abc", "abc") = true
       equalsIgnoreCase("abc", "ABC") = true
       
      参数:
      str1 - 要比较的字符串1
      str2 - 要比较的字符串2
      返回:
      如果两个字符串相同,或者都是null,则返回true
    • equals

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

      public static String fillAfter(String str, char filledChar, int len)
      将已有字符串填充为规定长度,如果已有字符串超过这个长度则返回这个字符串
      字符填充于字符串后
      参数:
      str - 被填充的字符串
      filledChar - 填充的字符
      len - 填充长度
      返回:
      填充后的字符串
      从以下版本开始:
      3.1.2
    • fill

      public static String fill(String str, char filledChar, int len, boolean isPre)
      将已有字符串填充为规定长度,如果已有字符串超过这个长度则返回这个字符串
      参数:
      str - 被填充的字符串
      filledChar - 填充的字符
      len - 填充长度
      isPre - 是否填充在前
      返回:
      填充后的字符串
      从以下版本开始:
      3.1.2
    • repeat

      public static String repeat(char c, int count)
      重复某个字符
      参数:
      c - 被重复的字符
      count - 重复的数目,如果小于等于0则返回""
      返回:
      重复字符字符串
    • trimStart

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

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

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

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

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

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

      public static int indexOf(CharSequence str, char searchChar)
      指定范围内查找指定字符
      参数:
      str - 字符串
      searchChar - 被查找的字符
      返回:
      位置
    • indexOf

      public static int indexOf(CharSequence str, char searchChar, int start)
      指定范围内查找指定字符
      参数:
      str - 字符串
      searchChar - 被查找的字符
      start - 起始位置,如果小于0,从0开始查找
      返回:
      位置
    • indexOf

      public static int indexOf(CharSequence str, char searchChar, int start, int end)
      指定范围内查找指定字符
      参数:
      str - 字符串
      searchChar - 被查找的字符
      start - 起始位置,如果小于0,从0开始查找
      end - 终止位置,如果超过str.length()则默认查找到字符串末尾
      返回:
      位置
    • indexOfIgnoreCase

      public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr)
      指定范围内查找字符串,忽略大小写
       StrUtil.indexOfIgnoreCase(null, *, *)          = -1
       StrUtil.indexOfIgnoreCase(*, null, *)          = -1
       StrUtil.indexOfIgnoreCase("", "", 0)           = 0
       StrUtil.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
       StrUtil.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
       StrUtil.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
       StrUtil.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
       StrUtil.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
       StrUtil.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
       StrUtil.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
       StrUtil.indexOfIgnoreCase("abc", "", 9)        = -1
       
      参数:
      str - 字符串
      searchStr - 需要查找位置的字符串
      返回:
      位置
      从以下版本开始:
      3.2.1
    • indexOfIgnoreCase

      public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr, int fromIndex)
      指定范围内查找字符串
       StrUtil.indexOfIgnoreCase(null, *, *)          = -1
       StrUtil.indexOfIgnoreCase(*, null, *)          = -1
       StrUtil.indexOfIgnoreCase("", "", 0)           = 0
       StrUtil.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
       StrUtil.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
       StrUtil.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
       StrUtil.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
       StrUtil.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
       StrUtil.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
       StrUtil.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
       StrUtil.indexOfIgnoreCase("abc", "", 9)        = -1
       
      参数:
      str - 字符串
      searchStr - 需要查找位置的字符串
      fromIndex - 起始位置
      返回:
      位置
      从以下版本开始:
      3.2.1
    • indexOf

      public static int indexOf(CharSequence str, CharSequence searchStr, int fromIndex, boolean ignoreCase)
      指定范围内反向查找字符串
      参数:
      str - 字符串
      searchStr - 需要查找位置的字符串
      fromIndex - 起始位置
      ignoreCase - 是否忽略大小写
      返回:
      位置
      从以下版本开始:
      3.2.1
    • lastIndexOfIgnoreCase

      public static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr)
      指定范围内查找字符串,忽略大小写
      参数:
      str - 字符串
      searchStr - 需要查找位置的字符串
      返回:
      位置
      从以下版本开始:
      3.2.1
    • lastIndexOfIgnoreCase

      public static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr, int fromIndex)
      指定范围内查找字符串,忽略大小写
      参数:
      str - 字符串
      searchStr - 需要查找位置的字符串
      fromIndex - 起始位置,从后往前计数
      返回:
      位置
      从以下版本开始:
      3.2.1
    • lastIndexOf

      public static int lastIndexOf(CharSequence str, CharSequence searchStr, int fromIndex, boolean ignoreCase)
      指定范围内查找字符串
      参数:
      str - 字符串
      searchStr - 需要查找位置的字符串
      fromIndex - 起始位置,从后往前计数
      ignoreCase - 是否忽略大小写
      返回:
      位置
      从以下版本开始:
      3.2.1
    • isSubEquals

      public static boolean isSubEquals(CharSequence str1, int start1, CharSequence str2, int start2, int length, boolean ignoreCase)
      截取两个字符串的不同部分(长度一致),判断截取的子串是否相同
      任意一个字符串为null返回false
      参数:
      str1 - 第一个字符串
      start1 - 第一个字符串开始的位置
      str2 - 第二个字符串
      start2 - 第二个字符串开始的位置
      length - 截取长度
      ignoreCase - 是否忽略大小写
      返回:
      子串是否相同
      从以下版本开始:
      3.2.1
    • startWith

      public static boolean startWith(CharSequence str, char c)
      字符串是否以给定字符开始
      参数:
      str - 字符串
      c - 字符
      返回:
      是否开始
    • startWith

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

      public static boolean endWith(CharSequence str, char c)
      字符串是否以给定字符结尾
      参数:
      str - 字符串
      c - 字符
      返回:
      是否结尾
    • endWith

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

      public static boolean endWith(CharSequence str, CharSequence suffix)
      是否以指定字符串结尾
      参数:
      str - 被监测字符串
      suffix - 结尾字符串
      返回:
      是否以指定字符串结尾
    • endWithIgnoreCase

      public static boolean endWithIgnoreCase(CharSequence str, CharSequence suffix)
      是否以指定字符串结尾,忽略大小写
      参数:
      str - 被监测字符串
      suffix - 结尾字符串
      返回:
      是否以指定字符串结尾
    • contains

      public static boolean contains(CharSequence str, char searchChar)
      指定字符是否在字符串中出现过
      参数:
      str - 字符串
      searchChar - 被查找的字符
      返回:
      是否包含
      从以下版本开始:
      3.1.2
    • containsIgnoreCase

      public static boolean containsIgnoreCase(CharSequence str, CharSequence testStr)
      是否包含特定字符,忽略大小写,如果给定两个参数都为null,返回true
      参数:
      str - 被检测字符串
      testStr - 被测试是否包含的字符串
      返回:
      是否包含
    • split

      public static String[] split(String str, String separator)
      参数:
      str -
      separator -
      返回:
    • utf8Str

      public static String utf8Str(Object obj)
      将对象转为字符串
      1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
      参数:
      obj - 对象
      返回:
      字符串
    • str

      public static String str(Object obj, String charsetName)
      将对象转为字符串
      1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
      参数:
      obj - 对象
      charsetName - 字符集
      返回:
      字符串
    • str

      public static String str(Object obj, Charset charset)
      将对象转为字符串
      1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
      参数:
      obj - 对象
      charset - 字符集
      返回:
      字符串
    • str

      public static String str(CharSequence cs)
      CharSequence 转为字符串,null安全
      参数:
      cs - CharSequence
      返回:
      字符串
    • 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(不包括)
      返回:
      字串
    • subPreGbk

      public static String subPreGbk(CharSequence str, int len, CharSequence suffix)
      截取部分字符串,这里一个汉字的长度认为是2
      参数:
      str - 字符串
      len - 切割的位置
      suffix - 切割后加上后缀
      返回:
      切割后的字符串
      从以下版本开始:
      3.1.1
    • maxLength

      public static String maxLength(CharSequence string, int length)
      限制字符串长度,如果超过指定长度,截取指定长度并在末尾加"..."
      参数:
      string - 字符串
      length - 最大长度
      返回:
      切割后的剩余的前半部分字符串+"..."
      从以下版本开始:
      4.0.10
    • subPre

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

      public static String subSuf(CharSequence string, int fromIndex)
      切割指定位置之后部分的字符串
      参数:
      string - 字符串
      fromIndex - 切割开始的位置(包括)
      返回:
      切割后后剩余的后半部分字符串
    • subSufByLength

      public static String subSufByLength(CharSequence string, int length)
      切割指定长度的后部分的字符串
       StrUtil.subSufByLength("abcde", 3)      =    "cde"
       StrUtil.subSufByLength("abcde", 0)      =    ""
       StrUtil.subSufByLength("abcde", -5)     =    ""
       StrUtil.subSufByLength("abcde", -1)     =    ""
       StrUtil.subSufByLength("abcde", 5)       =    "abcde"
       StrUtil.subSufByLength("abcde", 10)     =    "abcde"
       StrUtil.subSufByLength(null, 3)               =    null
       
      参数:
      string - 字符串
      length - 切割长度
      返回:
      切割后后剩余的后半部分字符串
      从以下版本开始:
      4.0.1
    • subWithLength

      public static String subWithLength(String input, int fromIndex, int length)
      截取字符串,从指定位置开始,截取指定长度的字符串
      author weibaohui
      参数:
      input - 原始字符串
      fromIndex - 开始的index,包括
      length - 要截取的长度
      返回:
      截取后的字符串
    • subBefore

      public static String subBefore(CharSequence string, CharSequence 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"
       StrUtil.subBefore("abc", "")    = ""
       StrUtil.subBefore("abc", null)  = "abc"
       
      参数:
      string - 被查找的字符串
      separator - 分隔字符串(不包括)
      isLastSeparator - 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个
      返回:
      切割后的字符串
      从以下版本开始:
      3.1.1
    • subAfter

      public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator)
      截取分隔字符串之后的字符串,不包括分隔字符串
      如果给定的字符串为空串(null或""),返回原字符串
      如果分隔字符串为空串(null或""),则返回空串,如果分隔字符串未找到,返回空串 栗子:
       StrUtil.subAfter(null, *)      = null
       StrUtil.subAfter("", *)        = ""
       StrUtil.subAfter(*, null)      = ""
       StrUtil.subAfter("abc", "a")   = "bc"
       StrUtil.subAfter("abcba", "b") = "cba"
       StrUtil.subAfter("abc", "c")   = ""
       StrUtil.subAfter("abc", "d")   = ""
       StrUtil.subAfter("abc", "")    = "abc"
       
      参数:
      string - 被查找的字符串
      separator - 分隔字符串(不包括)
      isLastSeparator - 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个
      返回:
      切割后的字符串
      从以下版本开始:
      3.1.1
    • subBetween

      public static String subBetween(CharSequence str, CharSequence before, CharSequence after)
      截取指定字符串中间部分,不包括标识字符串
      栗子:
       StrUtil.subBetween("wx[b]yz", "[", "]") = "b"
       StrUtil.subBetween(null, *, *)          = null
       StrUtil.subBetween(*, null, *)          = null
       StrUtil.subBetween(*, *, null)          = null
       StrUtil.subBetween("", "", "")          = ""
       StrUtil.subBetween("", "", "]")         = null
       StrUtil.subBetween("", "[", "]")        = null
       StrUtil.subBetween("yabcz", "", "")     = ""
       StrUtil.subBetween("yabcz", "y", "z")   = "abc"
       StrUtil.subBetween("yabczyabcz", "y", "z")   = "abc"
       
      参数:
      str - 被切割的字符串
      before - 截取开始的字符串标识
      after - 截取到的字符串标识
      返回:
      截取后的字符串
      从以下版本开始:
      3.1.1
    • subBetween

      public static String subBetween(CharSequence str, CharSequence beforeAndAfter)
      截取指定字符串中间部分,不包括标识字符串
      栗子:
       StrUtil.subBetween(null, *)            = null
       StrUtil.subBetween("", "")             = ""
       StrUtil.subBetween("", "tag")          = null
       StrUtil.subBetween("tagabctag", null)  = null
       StrUtil.subBetween("tagabctag", "")    = ""
       StrUtil.subBetween("tagabctag", "tag") = "abc"
       
      参数:
      str - 被切割的字符串
      beforeAndAfter - 截取开始和结束的字符串标识
      返回:
      截取后的字符串
      从以下版本开始:
      3.1.1
    • convert

      public static Object convert(Class<?> type, String value) throws Exception
      参数:
      type -
      value -
      返回:
      抛出:
      Exception
    • convert

      public static Object convert(Class<?> type, String[] values) throws Exception
      参数:
      type -
      values -
      返回:
      返回的也是一个数组
      抛出:
      Exception
    • containsAny

      public static boolean containsAny(CharSequence str, CharSequence... testStrs)
      查找指定字符串是否包含指定字符串列表中的任意一个字符串
      参数:
      str - 指定字符串
      testStrs - 需要检查的字符串数组
      返回:
      是否包含任意一个字符串
      从以下版本开始:
      3.2.0
    • getContainsStr

      public static String getContainsStr(CharSequence str, CharSequence... testStrs)
      查找指定字符串是否包含指定字符串列表中的任意一个字符串,如果包含返回找到的第一个字符串
      参数:
      str - 指定字符串
      testStrs - 需要检查的字符串数组
      返回:
      被包含的第一个字符串
      从以下版本开始:
      3.2.0
    • upperFirst

      public static String upperFirst(CharSequence str)
      大写首字母
      例如:str = name, return Name
      参数:
      str - 字符串
      返回:
      字符串
    • lowerFirst

      public static String lowerFirst(CharSequence str)
      小写首字母
      例如:str = Name, return name
      参数:
      str - 字符串
      返回:
      字符串
    • join

      public static String join(Collection<?> coll)
      以 , 为分隔符将多个对象转换为字符串
      参数:
      coll - 集合
      返回:
      连接后的字符串
    • join

      public static String join(Collection<?> coll, CharSequence delimiter)
      以 delimiter 为分隔符将多个对象转换为字符串
      参数:
      coll - 集合
      delimiter - 分隔符
      返回:
      连接后的字符串