类 StringUtil

java.lang.Object
me.codeplayer.util.StringUtil

public abstract class StringUtil extends Object
用于对字符串类型的数据进行常用处理操作的工具类
从以下版本开始:
2012-10-29
作者:
Ready
  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    protected static final char[]
    用于在2-16进制之间进行转换的映射字符数组
  • 构造器概要

    构造器
    构造器
    说明
     
  • 方法概要

    修饰符和类型
    方法
    说明
    append(StringBuilder sb, String... parts)
    拼接多个字符串(忽略其中为 null 的参数)
    static String
    将字符串的首字母大写
    static String
    拼接两个字符串(忽略其中为 null 的参数)
    static String
    拼接多个字符串(忽略其中为 null 的参数)
    static String
    concats(String... parts)
    拼接多个字符串(忽略其中为 null 的参数)
    static boolean
    containsWord(String container, String searchedWord, String separatorChars)
    检测指定字符串中是否存在指定的单词
    该方法采用快速模式,对于类似 containsWord("abc123,123", "123", ",") 等特殊情况无法保证100%可靠;如果想要保证可靠性,建议使用 containsWord(String, String, String, boolean)
    static boolean
    containsWord(String container, String search, String separatorChars, boolean fastMode)
    检测指定字符串中是否存在指定的单词
    static String
    convertCharset(String str, Charset originalCharset, Charset targetCharset)
    将字符串从指定字符集编码转换为目标字符集编码
    static String
    convertCharsetForURI(String str, Charset targetCharset)
    将指定的URI参数字符串转换为目标字符集编码
    本方法实际上是将字符串从ISO-8859-1编码转换为指定的目标编码
    static String
    将字符串的首字母小写
    static boolean
    endsWith(String str, char lastChar)
    判断指定字符串是否以指定的单个字符结尾
    static int[]
    ensureRangeSafe(int beginIndex, int endIndex, int length)
    确保起始和结束索引的范围边界安全,并返回范围安全的 [开始索引, 结束索引] 数组
    如果索引参数为负,自动加上 length,如果处理后的 beginIndex > endIndex,则自动对调位置
    方法起始索引或结束索引超出合理边界,方法内会尽可能地根据 length 进行调整:
    1、如果 beginIndex < -length ,则 beginIndex = 0
    2、如果 endIndex > length ,则 endIndex = length
    escape(StringBuilder sb, String str, char escapeChar, char[] escapedChars)
    使用转义字符转义字符串中指定的字符
    static String
    escape(String str, char escapeChar, char[] escapedChars)
    使用转义字符转义字符串中指定的字符
    static String
    将指定的用于LIKE语句的字符串转义,以防止SQL语句注入
    该方法默认使用'\'进行转义操作
    escapeSQLLike(StringBuilder sb, String likeStr, char escapeChar, boolean appendWildcardAtStart, boolean appendWildcardAtEnd)
    使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
    static String
    escapeSQLLike(String likeStr, boolean appendLikeWildcard)
    将指定的用于LIKE语句的字符串转义,以防止SQL语句注入
    该方法默认使用'\'进行转义操作
    static String
    escapeSQLLike(String likeStr, boolean appendWildcardAtStart, boolean appendWildcardAtEnd)
    使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
    static String
    escapeSQLLike(String likeStr, char escapeChar, boolean appendWildcardAtBoth)
    使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
    static String
    escapeSQLLike(String likeStr, char escapeChar, boolean appendWildcardAtStart, boolean appendWildcardAtEnd)
    使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
    static String
    获取指定字符串的Unicode编码,例如:"中国"将返回"中国"
    此方法返回的编码中,字母均采用大写形式,此外,由于编码的长度是已知的,本方法采用char数组作为字符容器,省略了StringBuilder追加时的长度判断以及封装损耗,性能更加优异
    getBuilder(int size, int shift)
    根据每个元素的平均大小和元素的格式创建一个适用于其容量的StringBuilder
    内部采用length << shift 来获取设置初始容量
    并且生成的StringBuilder的最小容量为16(StringBuilder的默认容量)
    getBuilder(int extraCapacity, CharSequence s1)
    根据将要追加的多个字符序列获得适当初始容量的 StringBuilder
    getBuilder(int extraCapacity, CharSequence s1, CharSequence s2)
    根据将要追加的多个字符序列获得适当初始容量的 StringBuilder
    getBuilder(int extraCapacity, CharSequence s1, CharSequence s2, CharSequence s3)
    根据将要追加的多个字符序列获得适当初始容量的 StringBuilder
    getBuilder(int extraCapacity, CharSequence s1, CharSequence s2, CharSequence s3, CharSequence s4)
    根据将要追加的多个字符序列获得适当初始容量的 StringBuilder
    static boolean
    hasBlank(Object... values)
    判断指定数组中是否为空的值(null、空字符串、空格字符串),如果是,将返回true
    本方法接受多个参数,如果其中有任意一个为空,就返回true
    本方法会去除两边的空格后再判断 如果指定的key不存在也返回true
    static boolean
    hasEmpty(Object... values)
    判断指定数组中是否存在为空的值(null、空字符串),如果是将返回true
    本方法接受多个参数,如果其中有任意一个为空,就返回true 如果指定的key不存在也返回true
    initBuilder(StringBuilder sb, int appendLength)
    初始化一个还可以添加 appendLength 个字符的 StringBuilder
    static boolean
    判断指定的字符序列数组是否存在不为空的元素
    如果数组中存在不为null、空字符串的元素,则返回true,否则返回false
    static boolean
    判断指定的字符串是否为空
    如果字符串为null、空字符串、空格字符串,则返回true
    注意:本方法会先去除字符串两边的空格,再判断
    static boolean
    判断指定的对象是否为空
    如果对象(或其 toString() 返回值)为null、空字符串、空格字符串,则返回true
    注意:本方法会先去除字符串两边的空格,再判断
    static boolean
    判断指定的字符串是否为空
    如果字符串为null、空字符串,则返回true,否则返回false
    注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用 isBlank(CharSequence) 方法
    static boolean
    判断指定的对象是否为空
    如果对象(或其 toString() 返回值)为null、空字符串,则返回true
    注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用isBlank(Object obj)方法
    static <E> String
    join(Collection<E> items, BiConsumer<StringBuilder,E> itemAppender, String delimiter)
    将集合的指定属性或输出拼接为字符串
    static <E> String
    join(Collection<E> c, Function<? super E,Object> getter, String delimiter)
    将集合的指定属性或输出拼接为字符串
    static <E> StringBuilder
    joinAppend(StringBuilder sb, Collection<E> items, BiConsumer<StringBuilder,E> itemAppender, String delimiter)
    将集合的指定属性或输出拼接为字符串
    static <T> String
    joinInt(Collection<T> c, ToIntFunction<? super T> mapper, String delimiter)
    将集合的指定整数(Integer)属性或输出拼接为字符串
    static <T> String
    joinLong(Collection<T> c, ToLongFunction<? super T> mapper, String delimiter)
    将集合的指定整数(Long)属性或输出拼接为字符串
    static <T> String
    joinNumber(Collection<T> c, Function<? super T,Number> mapper, String delimiter)
    将集合的指定整数(Long 或 Integer)属性或输出拼接为字符串
    static String
    leftPad(String str, char ch, int minLength)
    如果字符串不足指定位数,则在前面补充指定的字符,直到期望的最小位数
    如果字符串=null,则返回空字符串""
    如果字符串位数大于指定位数,则返回原字符串
    static int
    获取指定字符序列的字符长度
    static String
    limitChars(String str, int maxLength)
    如果指定字符串超过限制长度maxLength,则返回限制长度前面的部分字符串
    如果指定字符串==null,则返回空字符串
    如果字符串超出指定长度,则返回maxLength前面的部分,并在末尾加上后缀“...”
    static String
    limitChars(String str, int maxLength, String suffix)
    如果指定字符串超过限制长度maxLength,则返回限制长度前面的部分字符串
    如果指定字符串==null,则返回空字符串
    如果字符串超出指定长度,则返回maxLength前面的部分,并在末尾加上后缀suffix
    static boolean
    判断指定的字符串是否不为空
    如果字符串不为null、空字符串、空格字符串,则返回true,否则返回false
    注意:本方法会先去除字符串两边的空格,再判断
    static boolean
    判断指定的对象是否为空
    如果对象(或其 toString() 返回值)不为null、空字符串、空格字符串,则返回true,否则返回false
    注意:本方法会先去除字符串两边的空格,再判断
    static boolean
    判断指定的字符串是否不为空
    如果指定字符串不为null、空字符串,则返回true,否则返回false
    注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用 notBlank(CharSequence) 方法
    static boolean
    判断指定的对象是否不为空
    如果对象(或其 toString() 返回值)不为null、空字符串,则返回true,否则返回false
    注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用isBlank(Object obj)方法
    static String
    安全地获取指定字符序列中的字符串
    static String
    nullSafeGet(Supplier<? extends CharSequence> msgSupplier)
    安全地执行指定的字符串懒加载表达式,并返回对应的字符串
    static String
    replaceChar(String str, int charIndex, CharConverter converter)
    替换字符串中指定位置的字符,并返回替换后的结果
    static String
    replaceChars(String str, char ch, int beginIndex)
    将指定字符串的 beginIndex 到末尾之间的字符全部替换为字符 ch
    static String
    replaceChars(String str, char ch, int beginIndex, int endIndex)
    将指定字符串的 beginIndexendIndex (不包括 endIndex ) 之间的字符全部替换为字符 ch
    static String
    replaceSubstring(String str, String replacement, int beginIndex)
    将指定字符串的 beginIndex 及其后的子字符串全部替换为指定的字符串 replacement
    static String
    replaceSubstring(String str, String replacement, int beginIndex, int endIndex)
    将指定字符串的 beginIndexendIndex (不包括 endIndex ) 之间的子字符串全部替换为指定的字符串 replacement
    static String
    颠倒(反转)字符串的字符顺序,并返回颠倒后的字符串
    如果字符串为null,则返回空字符串""
    static String
    rightPad(String str, char ch, int maxLength)
    如果字符串不足指定位数,则在后面补充指定的字符,直到期望的最小位数
    如果字符串=null,则返回空字符串""
    如果字符串位数大于指定位数,则返回原字符串
    static <T> List<T>
    split(String toSplit, String sep, Function<? super String,T> mapper, boolean ignoreEmpty)
    将以指定分隔字符分隔字符串,并将拆分后的每个片段文本使用指定的 mapper 进行转换,并返回转换后的元素集合
    static <T> List<T>
    split(String toSplit, String sep, Function<String,T> mapper)
    将以指定分隔字符分隔字符串,并将拆分后的每个片段文本使用指定的 mapper 进行转换,并返回转换后的元素集合
    static <T> List<T>
    split(String str, String sep, Predicate<? super String> filter, Function<? super String,T> mapper)
    将以指定分隔字符分隔字符串,并将拆分后的每个片段文本使用指定的 mapper 进行转换,并返回转换后的元素集合
    static boolean
    startsWith(String str, char firstChar)
    判断指定字符串是否以指定的单个字符开头
    static int
    stringSize(long x)
    返回整数的字符数
    static String
    以String的形式返回对象值,如果对象为null,则返回""
    static String
    toString(Object obj, String defaultValIfNull)
    以String的形式返回对象值,如果对象为null,则返回""
    static String
    trim(Object obj)
    以去除两边空格的String形式返回对象值,如果对象为null,则返回""
    static String
    trim(String str)
    去除字符串两侧的空白字符
    如果为null则返回空字符串""
    static String
    去除字符串中所有的空白字符并返回处理后的字符串
    如果为null则返回空字符串 ""
    unescape(StringBuilder sb, String escapedStr, char escapeChar, char[] escapedChars)
    解除对使用 escape(StringBuilder, String, char, char[]) 方法转义后的字符串的转义
    static String
    unescape(String escapedStr, char escapeChar, char[] escapedChars)
    解除对使用 escape(StringBuilder, String, char, char[]) 方法转义后的字符串的转义
    static String
    unicode(String source)
    获取指定字符串的Unicode编码,例如:“中国”将返回“中国”
    此方法返回的编码中,字母均采用大写形式,此外,本方法采用 StringBuilder 作为字符容器
    static String
    zeroFill(int val, int minLength)
    将指定 int 数值转为字符串。
    static String
    zeroFill(long val, int minLength)
    将指定 long 数值转为字符串,如果位数不足,则在前面补0,直到满足期望的最小位数minLength
    zeroFill(StringBuilder sb, long val, int minLength)
    将指定数值追加到指定不足 StringBuilder 中指定位数,如果指定数值的位数不足,则在前面补0,直到达到期望的最小位数为止。
    static String
    zeroFill(String str, int minLength)
    如果字符串不足指定位数,则前面补0,直到指定位数
    如果字符串=null,则返回空字符串""
    如果字符串位数大于指定位数,则返回原字符串

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 字段详细资料

    • digits

      protected static final char[] digits
      用于在2-16进制之间进行转换的映射字符数组
  • 构造器详细资料

    • StringUtil

      public StringUtil()
  • 方法详细资料

    • unicode

      public static String unicode(String source)
      获取指定字符串的Unicode编码,例如:“中国”将返回“中国”
      此方法返回的编码中,字母均采用大写形式,此外,本方法采用 StringBuilder 作为字符容器
      参数:
      source - 指定字符串不能为 null,否则将引发空指针异常
      从以下版本开始:
      0.0.1
    • fastUnicode

      public static String fastUnicode(String source)
      获取指定字符串的Unicode编码,例如:"中国"将返回"中国"
      此方法返回的编码中,字母均采用大写形式,此外,由于编码的长度是已知的,本方法采用char数组作为字符容器,省略了StringBuilder追加时的长度判断以及封装损耗,性能更加优异
      参数:
      source - 指定字符串不能为null,否则将引发空指针异常
      从以下版本开始:
      0.0.1
    • getBuilder

      public static StringBuilder getBuilder(int size, int shift)
      根据每个元素的平均大小和元素的格式创建一个适用于其容量的StringBuilder
      内部采用length << shift 来获取设置初始容量
      并且生成的StringBuilder的最小容量为16(StringBuilder的默认容量)
      参数:
      size - 元素个数
      shift - 每个元素参与拼接的平均字符数相对于2的位移量
      从以下版本开始:
      0.0.1
    • length

      public static int length(@Nullable CharSequence cs)
      获取指定字符序列的字符长度
      返回:
      对应的字符长度,如果 csnull,则返回 0
      从以下版本开始:
      0.4.2
    • getBuilder

      public static StringBuilder getBuilder(int extraCapacity, @Nullable CharSequence s1, @Nullable CharSequence s2, @Nullable CharSequence s3, @Nullable CharSequence s4)
      根据将要追加的多个字符序列获得适当初始容量的 StringBuilder
      参数:
      extraCapacity - 除指定的字符序列外应额外预留的字符容量
      从以下版本开始:
      0.4.2
    • getBuilder

      public static StringBuilder getBuilder(int extraCapacity, @Nullable CharSequence s1, @Nullable CharSequence s2, @Nullable CharSequence s3)
      根据将要追加的多个字符序列获得适当初始容量的 StringBuilder
      参数:
      extraCapacity - 除指定的字符序列外应额外预留的字符容量
      从以下版本开始:
      0.4.2
    • getBuilder

      public static StringBuilder getBuilder(int extraCapacity, @Nullable CharSequence s1, @Nullable CharSequence s2)
      根据将要追加的多个字符序列获得适当初始容量的 StringBuilder
      参数:
      extraCapacity - 除指定的字符序列外应额外预留的字符容量
      从以下版本开始:
      0.4.2
    • getBuilder

      public static StringBuilder getBuilder(int extraCapacity, @Nullable CharSequence s1)
      根据将要追加的多个字符序列获得适当初始容量的 StringBuilder
      参数:
      extraCapacity - 除指定的字符序列外应额外预留的字符容量
      从以下版本开始:
      0.4.2
    • concat

      public static String concat(@Nullable String a, @Nullable String b)
      拼接两个字符串(忽略其中为 null 的参数)
      从以下版本开始:
      3.0.0
    • concat

      public static String concat(@Nullable String a, @Nullable String b, @Nullable String c)
      拼接多个字符串(忽略其中为 null 的参数)
      从以下版本开始:
      3.0.0
    • append

      public static StringBuilder append(@Nullable StringBuilder sb, String... parts)
      拼接多个字符串(忽略其中为 null 的参数)
      从以下版本开始:
      3.0.0
    • concats

      public static String concats(String... parts)
      拼接多个字符串(忽略其中为 null 的参数)
      从以下版本开始:
      3.0.0
    • initBuilder

      public static StringBuilder initBuilder(@Nullable StringBuilder sb, int appendLength)
      初始化一个还可以添加 appendLength 个字符的 StringBuilder
      参数:
      sb - 如果为null,内部会自动创建
      appendLength - 还需要扩展的字符数
      从以下版本开始:
      3.0.0
    • isEmpty

      public static boolean isEmpty(@Nullable CharSequence cs)
      判断指定的字符串是否为空
      如果字符串为null、空字符串,则返回true,否则返回false
      注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用 isBlank(CharSequence) 方法
      从以下版本开始:
      0.0.1
    • notEmpty

      public static boolean notEmpty(@Nullable CharSequence cs)
      判断指定的字符串是否不为空
      如果指定字符串不为null、空字符串,则返回true,否则返回false
      注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用 notBlank(CharSequence) 方法
      从以下版本开始:
      0.0.1
    • isAnyNotEmpty

      public static boolean isAnyNotEmpty(@Nullable CharSequence... css)
      判断指定的字符序列数组是否存在不为空的元素
      如果数组中存在不为null、空字符串的元素,则返回true,否则返回false
      从以下版本开始:
      1.0.2
    • isEmpty

      public static boolean isEmpty(@Nullable Object obj)
      判断指定的对象是否为空
      如果对象(或其 toString() 返回值)为null、空字符串,则返回true
      注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用isBlank(Object obj)方法
      参数:
      obj - 指定的对象
      从以下版本开始:
      0.0.1
    • notEmpty

      public static boolean notEmpty(@Nullable Object obj)
      判断指定的对象是否不为空
      如果对象(或其 toString() 返回值)不为null、空字符串,则返回true,否则返回false
      注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用isBlank(Object obj)方法
      参数:
      obj - 指定的对象
      从以下版本开始:
      0.0.1
    • hasEmpty

      public static boolean hasEmpty(Object... values)
      判断指定数组中是否存在为空的值(null、空字符串),如果是将返回true
      本方法接受多个参数,如果其中有任意一个为空,就返回true 如果指定的key不存在也返回true
      参数:
      values - 指定的数组
      另请参阅:
    • isBlank

      public static boolean isBlank(CharSequence str)
      判断指定的字符串是否为空
      如果字符串为null、空字符串、空格字符串,则返回true
      注意:本方法会先去除字符串两边的空格,再判断
      从以下版本开始:
      0.0.1
    • notBlank

      public static boolean notBlank(CharSequence cs)
      判断指定的字符串是否不为空
      如果字符串不为null、空字符串、空格字符串,则返回true,否则返回false
      注意:本方法会先去除字符串两边的空格,再判断
      从以下版本开始:
      0.0.1
    • isBlank

      public static boolean isBlank(Object obj)
      判断指定的对象是否为空
      如果对象(或其 toString() 返回值)为null、空字符串、空格字符串,则返回true
      注意:本方法会先去除字符串两边的空格,再判断
      参数:
      obj - 指定的对象
      从以下版本开始:
      0.0.1
    • notBlank

      public static boolean notBlank(Object obj)
      判断指定的对象是否为空
      如果对象(或其 toString() 返回值)不为null、空字符串、空格字符串,则返回true,否则返回false
      注意:本方法会先去除字符串两边的空格,再判断
      参数:
      obj - 指定的对象
      从以下版本开始:
      0.0.1
    • hasBlank

      public static boolean hasBlank(Object... values)
      判断指定数组中是否为空的值(null、空字符串、空格字符串),如果是,将返回true
      本方法接受多个参数,如果其中有任意一个为空,就返回true
      本方法会去除两边的空格后再判断 如果指定的key不存在也返回true
      参数:
      values - 指定的数组
      从以下版本开始:
      0.0.1
    • toString

      public static String toString(Object obj)
      以String的形式返回对象值,如果对象为null,则返回""
      参数:
      obj - 指定的对象
      从以下版本开始:
      0.0.1
    • nullSafeGet

      @Nullable public static String nullSafeGet(@Nullable Supplier<? extends CharSequence> msgSupplier)
      安全地执行指定的字符串懒加载表达式,并返回对应的字符串
    • nullSafeGet

      @Nullable public static String nullSafeGet(@Nullable CharSequence cs)
      安全地获取指定字符序列中的字符串
      参数:
      cs - 如果该参数为 null,则返回 null
    • toString

      public static String toString(Object obj, String defaultValIfNull)
      以String的形式返回对象值,如果对象为null,则返回""
      参数:
      obj - 指定的对象
      从以下版本开始:
      0.0.1
    • trim

      public static String trim(Object obj)
      以去除两边空格的String形式返回对象值,如果对象为null,则返回""
      参数:
      obj - 指定的对象
      从以下版本开始:
      0.0.1
    • limitChars

      public static String limitChars(String str, int maxLength, String suffix)
      如果指定字符串超过限制长度maxLength,则返回限制长度前面的部分字符串
      如果指定字符串==null,则返回空字符串
      如果字符串超出指定长度,则返回maxLength前面的部分,并在末尾加上后缀suffix
      参数:
      str - 指定的字符串
      maxLength - 最大限制长度
      suffix - 超出长度时添加的指定后缀,如果不需要,可以为null
      从以下版本开始:
      0.0.1
    • limitChars

      public static String limitChars(String str, int maxLength)
      如果指定字符串超过限制长度maxLength,则返回限制长度前面的部分字符串
      如果指定字符串==null,则返回空字符串
      如果字符串超出指定长度,则返回maxLength前面的部分,并在末尾加上后缀“...”
      参数:
      str - 指定的字符串
      maxLength - 最大限制长度
      从以下版本开始:
      0.0.1
    • zeroFill

      public static String zeroFill(String str, int minLength)
      如果字符串不足指定位数,则前面补0,直到指定位数
      如果字符串=null,则返回空字符串""
      如果字符串位数大于指定位数,则返回原字符串
      参数:
      str - 字符串
      minLength - 期望的最小字符数,不能小于1
      从以下版本开始:
      0.0.1
    • stringSize

      public static int stringSize(long x)
      返回整数的字符数
      另请参阅:
      • Long.stringSize(long)
    • zeroFill

      public static StringBuilder zeroFill(@Nullable StringBuilder sb, long val, int minLength)
      将指定数值追加到指定不足 StringBuilder 中指定位数,如果指定数值的位数不足,则在前面补0,直到达到期望的最小位数为止。
      参数:
      sb - 用于拼接字符串的 StringBuilder 对象,如果为 null 则内部自动新建
      val - 数值
      minLength - 期望的最小位数,不应小于1
      从以下版本开始:
      3.9.0
    • zeroFill

      public static String zeroFill(long val, int minLength)
      将指定 long 数值转为字符串,如果位数不足,则在前面补0,直到满足期望的最小位数minLength
      参数:
      val - 数值
      minLength - 期望的最小位数,不应小于1
      从以下版本开始:
      3.9.0
    • zeroFill

      public static String zeroFill(int val, int minLength)
      将指定 int 数值转为字符串。如果位数不足,则在前面补0,直到满足期望的最小位数minLength
      参数:
      val - 数值
      minLength - 期望的最小位数,不应小于1
      从以下版本开始:
      3.9.0
    • leftPad

      public static String leftPad(String str, char ch, int minLength)
      如果字符串不足指定位数,则在前面补充指定的字符,直到期望的最小位数
      如果字符串=null,则返回空字符串""
      如果字符串位数大于指定位数,则返回原字符串
      参数:
      str - 字符串
      ch - 指定的字符
      minLength - 期望的最小位数,不能小于1
      从以下版本开始:
      0.0.1
    • rightPad

      public static String rightPad(String str, char ch, int maxLength)
      如果字符串不足指定位数,则在后面补充指定的字符,直到期望的最小位数
      如果字符串=null,则返回空字符串""
      如果字符串位数大于指定位数,则返回原字符串
      参数:
      str - 字符串
      ch - 指定的字符
      maxLength - 期望的最小位数,不能小于1
      从以下版本开始:
      0.0.1
    • trim

      public static String trim(String str)
      去除字符串两侧的空白字符
      如果为null则返回空字符串""
      从以下版本开始:
      0.0.1
    • trimAll

      public static String trimAll(String str)
      去除字符串中所有的空白字符并返回处理后的字符串
      如果为null则返回空字符串 ""
      参数:
      str - 指定的字符串
      从以下版本开始:
      3.8.0
    • replaceChars

      public static String replaceChars(String str, char ch, int beginIndex, int endIndex)
      将指定字符串的 beginIndexendIndex (不包括 endIndex ) 之间的字符全部替换为字符 ch
      参数:
      str - 指定的字符串
      ch - 指定的字符
      beginIndex - 指定的字符串起始索引(可以为负数,表示 beginIndex + str.length() )
      endIndex - 指定的字符串结束索引(可以为负数,表示 endIndex + str.length() )
      从以下版本开始:
      0.1.1
    • ensureRangeSafe

      public static int[] ensureRangeSafe(int beginIndex, int endIndex, int length)
      确保起始和结束索引的范围边界安全,并返回范围安全的 [开始索引, 结束索引] 数组
      如果索引参数为负,自动加上 length,如果处理后的 beginIndex > endIndex,则自动对调位置
      方法起始索引或结束索引超出合理边界,方法内会尽可能地根据 length 进行调整:
      1、如果 beginIndex < -length ,则 beginIndex = 0
      2、如果 endIndex > length ,则 endIndex = length
      参数:
      beginIndex - 起始索引
      endIndex - 结束索引
      length - 指定数组、集合或字符串的长度,不能小于0
      返回:
      如果范围安全则返回对应的范围数组,否则返回null
      从以下版本开始:
      0.4.2
    • replaceChars

      public static String replaceChars(String str, char ch, int beginIndex)
      将指定字符串的 beginIndex 到末尾之间的字符全部替换为字符 ch
      参数:
      str - 指定的字符串
      ch - 指定的字符
      beginIndex - 指定的字符串起始索引(可以为负数,表示 beginIndex + str.length() )
      从以下版本开始:
      0.1.1
    • replaceSubstring

      public static String replaceSubstring(String str, String replacement, int beginIndex, int endIndex)
      将指定字符串的 beginIndexendIndex (不包括 endIndex ) 之间的子字符串全部替换为指定的字符串 replacement
      参数:
      str - 指定的字符串
      replacement - 指定的字符串
      beginIndex - 指定的字符串起始索引(可以为负数,表示 beginIndex + str.length() )
      endIndex - 指定的字符串结束索引(可以为负数,表示 endIndex + str.length() )
      从以下版本开始:
      0.1.1
    • replaceSubstring

      public static String replaceSubstring(String str, String replacement, int beginIndex)
      将指定字符串的 beginIndex 及其后的子字符串全部替换为指定的字符串 replacement
      参数:
      str - 指定的字符串
      replacement - 指定的字符串
      beginIndex - 指定的字符串起始索引(可以为负数,表示 beginIndex + str.length() )
      从以下版本开始:
      0.1.1
    • convertCharset

      public static String convertCharset(String str, Charset originalCharset, Charset targetCharset)
      将字符串从指定字符集编码转换为目标字符集编码
      参数:
      str - 指定的字符串
      originalCharset - 原始字符集编码
      targetCharset - 目标字符集编码
      从以下版本开始:
      0.3.9
    • convertCharsetForURI

      public static String convertCharsetForURI(String str, Charset targetCharset)
      将指定的URI参数字符串转换为目标字符集编码
      本方法实际上是将字符串从ISO-8859-1编码转换为指定的目标编码
      参数:
      str - 指定的URI参数字符串
      targetCharset - 目标字符集编码
      从以下版本开始:
      0.3.9
    • reverse

      public static String reverse(CharSequence str)
      颠倒(反转)字符串的字符顺序,并返回颠倒后的字符串
      如果字符串为null,则返回空字符串""
      参数:
      str - 指定的字符串
      从以下版本开始:
      0.0.1
    • startsWith

      public static boolean startsWith(String str, char firstChar)
      判断指定字符串是否以指定的单个字符开头
      参数:
      str - 指定的字符串
      firstChar - 指定的单个字符
      从以下版本开始:
      0.4.2
    • endsWith

      public static boolean endsWith(String str, char lastChar)
      判断指定字符串是否以指定的单个字符结尾
      参数:
      str - 指定的字符串
      lastChar - 指定的单个字符
      从以下版本开始:
      0.4.2
    • escape

      public static StringBuilder escape(@Nullable StringBuilder sb, String str, char escapeChar, char[] escapedChars)
      使用转义字符转义字符串中指定的字符
      参数:
      sb - 用于字符拼接的 StringBuilder
      str - 需要转义的字符串
      escapeChar - 转义字符
      escapedChars - 需要被转义的字符的数组
    • escape

      public static String escape(String str, char escapeChar, char[] escapedChars)
      使用转义字符转义字符串中指定的字符
      参数:
      str - 需要转义的字符串
      escapeChar - 转义字符
      escapedChars - 需要被转义的字符的数组
    • unescape

      public static StringBuilder unescape(@Nullable StringBuilder sb, String escapedStr, char escapeChar, char[] escapedChars)
      解除对使用 escape(StringBuilder, String, char, char[]) 方法转义后的字符串的转义
      参数:
      escapedStr - 被转义后的字符串
      escapeChar - 转义字符
      escapedChars - 已经被转义过的字符的数组
    • unescape

      public static String unescape(String escapedStr, char escapeChar, char[] escapedChars)
      解除对使用 escape(StringBuilder, String, char, char[]) 方法转义后的字符串的转义
      参数:
      escapedStr - 被转义后的字符串
      escapeChar - 转义字符
      escapedChars - 已经被转义过的字符的数组
    • escapeSQLLike

      @Nullable public static StringBuilder escapeSQLLike(@Nullable StringBuilder sb, String likeStr, char escapeChar, boolean appendWildcardAtStart, boolean appendWildcardAtEnd)
      使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
      参数:
      sb - 用于拼接字符的 StringBuilder,如果为 null,则内部会在需要时自动创建
      likeStr - 指定的字符串
      escapeChar - 转义字符
      appendWildcardAtStart - 是否需要在字符串开头添加通配符'%'
      appendWildcardAtEnd - 是否需要在字符串末尾添加通配符'%'
      返回:
      如果 sb 为 null,且 likeStr 为空时,返回 null
      从以下版本开始:
      3.0.0
    • escapeSQLLike

      public static String escapeSQLLike(String likeStr, char escapeChar, boolean appendWildcardAtStart, boolean appendWildcardAtEnd)
      使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
      参数:
      likeStr - 指定的字符串
      escapeChar - 转义字符
      appendWildcardAtStart - 是否需要在字符串开头添加通配符'%'
      appendWildcardAtEnd - 是否需要在字符串末尾添加通配符'%'
      从以下版本开始:
      2.9
    • escapeSQLLike

      public static String escapeSQLLike(String likeStr, boolean appendWildcardAtStart, boolean appendWildcardAtEnd)
      使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
      参数:
      likeStr - 指定的字符串
      appendWildcardAtStart - 是否需要在字符串开头添加通配符'%'
      appendWildcardAtEnd - 是否需要在字符串末尾添加通配符'%'
      从以下版本开始:
      2.9
    • escapeSQLLike

      public static String escapeSQLLike(String likeStr, char escapeChar, boolean appendWildcardAtBoth)
      使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
      参数:
      likeStr - 指定的字符串
      escapeChar - 转义字符
      appendWildcardAtBoth - 是否需要在字符串两侧都添加通配符'%'
      从以下版本开始:
      0.3.5
    • escapeSQLLike

      public static String escapeSQLLike(String likeStr, boolean appendLikeWildcard)
      将指定的用于LIKE语句的字符串转义,以防止SQL语句注入
      该方法默认使用'\'进行转义操作
      参数:
      likeStr - 指定的字符串
      appendLikeWildcard - 是否需要在字符串两侧添加通配符'%'
      从以下版本开始:
      0.3.5
    • escapeSQLLike

      public static String escapeSQLLike(String likeStr)
      将指定的用于LIKE语句的字符串转义,以防止SQL语句注入
      该方法默认使用'\'进行转义操作
      参数:
      likeStr - 指定的字符串
      从以下版本开始:
      0.3.5
    • containsWord

      public static boolean containsWord(String container, String search, String separatorChars, boolean fastMode)
      检测指定字符串中是否存在指定的单词
      参数:
      container - 待检测的字符串
      search - 指定的单词
      separatorChars - 单词两侧必须是指定的字符之一或位于字符串 container 的首/尾位置
      fastMode - 是否启用快速模式。快速模式:如果在 container 中第一次检索到该单词,就直接在此处进行周边字符的匹配测试,并返回测试结果。
      哪怕后面还会再次出现该单词,也不再继续向后检查。请参考重载方法 containsWord(String, String, String) 方法上的注释
      从以下版本开始:
      2.0.0
    • containsWord

      public static boolean containsWord(String container, String searchedWord, String separatorChars)
      检测指定字符串中是否存在指定的单词
      该方法采用快速模式,对于类似 containsWord("abc123,123", "123", ",") 等特殊情况无法保证100%可靠;如果想要保证可靠性,建议使用 containsWord(String, String, String, boolean)
      参数:
      container - 待检测的字符串
      searchedWord - 指定的单词
      separatorChars - 单词两侧必须是指定的字符之一或位于字符串 container 的首/尾位置
      从以下版本开始:
      0.4.2
      另请参阅:
    • capitalize

      public static String capitalize(String str)
      将字符串的首字母大写
    • decapitalize

      public static String decapitalize(String str)
      将字符串的首字母小写
    • replaceChar

      public static String replaceChar(String str, int charIndex, CharConverter converter) throws IndexOutOfBoundsException
      替换字符串中指定位置的字符,并返回替换后的结果
      抛出:
      IndexOutOfBoundsException - 索引越界时会抛出该异常。不过请注意:如果 str 为 null 时,将直接返回 null,而不会抛出 NPE
    • joinAppend

      public static <E> StringBuilder joinAppend(@Nonnull StringBuilder sb, Collection<E> items, BiConsumer<StringBuilder,E> itemAppender, String delimiter)
      将集合的指定属性或输出拼接为字符串
      参数:
      delimiter - 分隔符
    • join

      public static <E> String join(Collection<E> items, BiConsumer<StringBuilder,E> itemAppender, String delimiter)
      将集合的指定属性或输出拼接为字符串
      参数:
      delimiter - 分隔符
    • join

      public static <E> String join(Collection<E> c, Function<? super E,Object> getter, String delimiter)
      将集合的指定属性或输出拼接为字符串
      参数:
      delimiter - 分隔符
    • joinNumber

      public static <T> String joinNumber(Collection<T> c, Function<? super T,Number> mapper, String delimiter)
      将集合的指定整数(Long 或 Integer)属性或输出拼接为字符串
      参数:
      delimiter - 分隔符
    • joinLong

      public static <T> String joinLong(Collection<T> c, ToLongFunction<? super T> mapper, String delimiter)
      将集合的指定整数(Long)属性或输出拼接为字符串
      参数:
      delimiter - 分隔符
    • joinInt

      public static <T> String joinInt(Collection<T> c, ToIntFunction<? super T> mapper, String delimiter)
      将集合的指定整数(Integer)属性或输出拼接为字符串
      参数:
      delimiter - 分隔符
    • split

      public static <T> List<T> split(String str, String sep, @Nullable Predicate<? super String> filter, Function<? super String,T> mapper)
      将以指定分隔字符分隔字符串,并将拆分后的每个片段文本使用指定的 mapper 进行转换,并返回转换后的元素集合
      参数:
      str - 需要被拆分的字符串
      sep - 分隔符
      mapper - 转换器
      filter - 过滤器(如果应用到对应的元素返回 false,则返回的集合中不会包含该元素 )
    • split

      public static <T> List<T> split(String toSplit, String sep, Function<? super String,T> mapper, boolean ignoreEmpty)
      将以指定分隔字符分隔字符串,并将拆分后的每个片段文本使用指定的 mapper 进行转换,并返回转换后的元素集合
      参数:
      toSplit - 需要被拆分的字符串
      sep - 分隔符
      mapper - 转换器
      ignoreEmpty - 是否忽略空子字符串(如果为 true,则忽略空字符串 )
    • split

      public static <T> List<T> split(String toSplit, String sep, Function<String,T> mapper)
      将以指定分隔字符分隔字符串,并将拆分后的每个片段文本使用指定的 mapper 进行转换,并返回转换后的元素集合
      参数:
      toSplit - 需要被拆分的字符串
      sep - 分隔符
      mapper - 转换器