类 StrUtil


  • public class StrUtil
    extends StrSplitter
    String 工具类

    参考 hutool

    作者:
    思追(shaco)
    • 字段详细资料

      • DOUBLE_DOT

        public static final String DOUBLE_DOT
        字符串常量:双点 ".."
        用途:作为指向上级文件夹的路径,如:"../path"
        另请参阅:
        常量字段值
      • BACKSLASH

        public static final String BACKSLASH
        字符串常量:反斜杠 "\\"
        另请参阅:
        常量字段值
      • CR

        public static final String CR
        字符串常量:回车符 "\r"
        解释:该字符常用于表示 Linux 系统和 MacOS 系统下的文本换行
        另请参阅:
        常量字段值
      • CRLF

        public static final String CRLF
        字符串常量:Windows 换行 "\r\n"
        解释:该字符串常用于表示 Windows 系统下的文本换行
        另请参阅:
        常量字段值
      • UNDERLINE

        public static final String UNDERLINE
        字符串常量:下划线 "_"
        另请参阅:
        常量字段值
      • DASHED

        public static final String DASHED
        字符串常量:减号(连接符) "-"
        另请参阅:
        常量字段值
      • DELIM_START

        public static final String DELIM_START
        字符串常量:花括号(左) "{"
        另请参阅:
        常量字段值
      • DELIM_END

        public static final String DELIM_END
        字符串常量:花括号(右) "}"
        另请参阅:
        常量字段值
      • BRACKET_START

        public static final String BRACKET_START
        字符串常量:中括号(左) "["
        另请参阅:
        常量字段值
      • BRACKET_END

        public static final String BRACKET_END
        字符串常量:中括号(右) "]"
        另请参阅:
        常量字段值
      • EMPTY_JSON

        public static final String EMPTY_JSON
        字符串常量:空 JSON "{}"
        另请参阅:
        常量字段值
      • NULL

        public static final String NULL
        字符串常量:"null"
        注意:"null" != null
        另请参阅:
        常量字段值
      • EMPTY

        public static final String EMPTY
        字符串常量:空字符串 ""
        另请参阅:
        常量字段值
    • 构造器详细资料

      • StrUtil

        public StrUtil()
    • 方法详细资料

      • 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 - 是否忽略大小写
        返回:
        子串是否相同
      • isEmpty

        public static boolean isEmpty​(CharSequence str)

        字符串是否为空,空的定义如下:

        1. null
        2. 空字符串:""

        例:

        • StrUtil.isEmpty(null) // true
        • StrUtil.isEmpty("") // true
        • StrUtil.isEmpty(" \t\n") // false
        • StrUtil.isEmpty("abc") // false

        注意:该方法与 isBlank(CharSequence) 的区别是:该方法不校验空白字符。

        建议:

        • 该方法建议用于工具类或任何可以预期的方法参数的校验中。
        参数:
        str - 被检测的字符串
        返回:
        是否为空
        另请参阅:
        isBlank(CharSequence)
      • isNotEmpty

        public static boolean isNotEmpty​(CharSequence str)

        字符串是否为非空白,非空白的定义如下:

        1. 不为 null
        2. 不为空字符串:""

        例:

        • StrUtil.isNotEmpty(null) // false
        • StrUtil.isNotEmpty("") // false
        • StrUtil.isNotEmpty(" \t\n") // true
        • StrUtil.isNotEmpty("abc") // true

        注意:该方法与 isNotBlank(CharSequence) 的区别是:该方法不校验空白字符。

        建议:该方法建议用于工具类或任何可以预期的方法参数的校验中。

        参数:
        str - 被检测的字符串
        返回:
        是否为非空
        另请参阅:
        isEmpty(CharSequence)
      • isBlank

        public static boolean isBlank​(CharSequence str)

        字符串是否为空白,空白的定义如下:

        1. null
        2. 空字符串:""
        3. 空格、全角空格、制表符、换行符,等不可见字符

        例:

        • StrUtil.isBlank(null) // true
        • StrUtil.isBlank("") // true
        • StrUtil.isBlank(" \t\n") // true
        • StrUtil.isBlank("abc") // false
        参数:
        str - 被检测的字符串
        返回:
        若为空白,则返回 true
      • join

        public static String join​(Object[] arr,
                                  String delimiter)
        将一个 数组转换为一个带分隔符的String
        参数:
        arr - 数组元素
        delimiter - 要使用的分隔符(通常是",")。
        返回:
        带分隔符的String
      • isEmptyIfStr

        public static boolean isEmptyIfStr​(Object obj)

        如果对象是字符串是否为空串,空的定义如下:


        1. null
        2. 空字符串:""

        例:

        • StrUtil.isEmptyIfStr(null) // true
        • StrUtil.isEmptyIfStr("") // true
        • StrUtil.isEmptyIfStr(" \t\n") // false
        • StrUtil.isEmptyIfStr("abc") // false
        参数:
        obj - 对象
        返回:
        如果为字符串是否为空串
      • trimToNull

        @Nullable
        public static @Nullable String trimToNull​(CharSequence str)
        除去字符串头尾部的空白,如果字符串是null或者"",返回null
         StrUtil.trimToNull(null)          = null
         StrUtil.trimToNull("")            = null
         StrUtil.trimToNull("     ")       = null
         StrUtil.trimToNull("abc")         = "abc"
         StrUtil.trimToEmpty("    abc    ") = "abc"
         
        参数:
        str - 字符串
        返回:
        去除两边空白符后的字符串, 如果为空返回null
      • trimStart

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

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

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

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

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

         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
      • trim

        public static String trim​(CharSequence str,
                                  int mode,
                                  java.util.function.Predicate<Character> predicate)
        按照断言,除去字符串头尾部的断言为真的字符,如果字符串是null,依然返回null
        参数:
        str - 要处理的字符串
        mode - -1表示trimStart,0表示trim全部, 1表示trimEnd
        predicate - 断言是否过掉字符,返回true表述过滤掉,false表示不过滤
        返回:
        除去指定字符后的的字符串,如果原字串为null,则返回null
      • length

        public static int length​(CharSequence cs)
        获取字符串的长度,如果为null返回0
        参数:
        cs - 字符串
        返回:
        字符串的长度,如果为null返回0
      • endWith

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

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

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

        public static boolean endWith​(CharSequence str,
                                      CharSequence suffix)
        是否以指定字符串结尾
        参数:
        str - 被监测字符串
        suffix - 结尾字符串
        返回:
        是否以指定字符串结尾
      • 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
      • trimLeadingCharacter

        public static String trimLeadingCharacter​(String str,
                                                  char leadingCharacter)
        从给定的 字符串 中修剪提供的前导字符的所有匹配项
        参数:
        str - 要检查的 字符串
        leadingCharacter - 要修剪的字节
        返回:
        修剪后的 字符串
      • trimTrailingCharacter

        public static String trimTrailingCharacter​(String str,
                                                   char trailingCharacter)
        从给定的 字符串 中修剪提供的所有尾随字符
        参数:
        str - 要检查的 字符串
        trailingCharacter - 要修剪的尾随字符
        返回:
        修剪后的 字符串
      • cleanBlank

        public static String cleanBlank​(CharSequence str)
        清理空白字符
        参数:
        str - 被清理的字符串
        返回:
        清理后的字符串
      • filter

        public static String filter​(CharSequence str,
                                    java.util.function.Predicate<Character> filter)
        过滤字符串
        参数:
        str - 字符串
        filter - 过滤器,Predicate.test(Object) 返回为true的保留字符
        返回:
        过滤后的字符串
      • removeSuffix

        public static String removeSuffix​(CharSequence str,
                                          CharSequence suffix)
        去掉指定后缀
        参数:
        str - 字符串
        suffix - 后缀
        返回:
        切掉后的字符串,若后缀不是 suffix, 返回原字符串
      • removePrefix

        public static String removePrefix​(CharSequence str,
                                          CharSequence prefix)
        去掉指定前缀
        参数:
        str - 字符串
        prefix - 前缀
        返回:
        切掉后的字符串,若前缀不是 prefix, 返回原字符串
      • subPre

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

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

        public static String sub​(CharSequence str,
                                 int fromIndexInclude,
                                 int toIndexExclude)
        改进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
        fromIndexInclude - 开始的index(包括)
        toIndexExclude - 结束的index(不包括)
        返回:
        字串
      • toStringOrNull

        public static String toStringOrNull​(Object obj)
        调用对象的toString方法,null会返回null
        参数:
        obj - 对象
        返回:
        字符串 or null
      • startWithIgnoreCase

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

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

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

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

        public static boolean startWith​(CharSequence str,
                                        CharSequence prefix,
                                        boolean ignoreCase,
                                        boolean ignoreEquals)
        是否以指定字符串开头
        如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false
             CharSequenceUtil.startWith("123", "123", false, true);   -- false
             CharSequenceUtil.startWith("ABCDEF", "abc", true, true); -- true
             CharSequenceUtil.startWith("abc", "abc", true, true);    -- false
         
        参数:
        str - 被监测字符串
        prefix - 开头字符串
        ignoreCase - 是否忽略大小写
        ignoreEquals - 是否忽略字符串相等的情况
        返回:
        是否以指定字符串开头
      • lowerFirst

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

        public static String toCamelCase​(CharSequence name)
        将下划线方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。
        规则为:
        • 单字之间不以空格或任何连接符断开
        • 第一个单字首字母采用小写字母
        • 后续单字的首字母亦用大写字母
        例如:hello_world=》helloWorld 小写开头
        参数:
        name - 转换前的下划线大写方式命名的字符串
        返回:
        转换后的驼峰式命名的字符串
      • toCamelCase

        public static String toCamelCase​(CharSequence name,
                                         char symbol)
        将连接符方式命名的字符串转换为驼峰式。如果转换前的字符串为空,则返回空字符串。
        参数:
        name - 转换前的自定义方式命名的字符串
        symbol - 原字符串中的连接符连接符
        返回:
        转换后的驼峰式命名的字符串
      • contains

        public static boolean contains​(CharSequence str,
                                       char searchChar)
        指定字符是否在字符串中出现过
        参数:
        str - 字符串
        searchChar - 被查找的字符
        返回:
        是否包含
      • 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 text,
                                  char searchChar,
                                  int start,
                                  int end)
        指定范围内查找指定字符
        参数:
        text - 字符串
        searchChar - 被查找的字符
        start - 起始位置,如果小于0,从0开始查找
        end - 终止位置,如果超过str.length()则默认查找到字符串末尾
        返回:
        位置
      • substringMatch

        public static boolean substringMatch​(@NotNull
                                             @NotNull CharSequence str,
                                             int index,
                                             @NotNull
                                             @NotNull CharSequence substring)
        测试给定字符串是否与给定索引处的给定子字符串匹配
        参数:
        str - 原始字符串
        index - 原始字符串中要开始匹配的索引
        substring - 要在给定索引处匹配的子字符串
      • removePrefixIgnoreCase

        public static String removePrefixIgnoreCase​(CharSequence str,
                                                    CharSequence prefix)
        忽略大小写去掉指定前缀
        参数:
        str - 字符串
        prefix - 前缀
        返回:
        切掉后的字符串,若前缀不是 prefix, 返回原字符串