该对象提供了String字符串操作的一些常用方法。
目录
appendIfMissing(如果字符串没以某个字符串结尾为结尾,就添加该结尾)
字符串判断
方法名 | 使用 | 比较 |
isBlank | StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false |
没有字符就返回true,空格也返回空 |
isEmpty |
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false |
含有空格,不反悔空 |
与之对应的还有 (这里的”空“ 都泛指上面表格对应方法的true)
isNotBlank(CharSequence cs) isNotEmpty(CharSequence cs) |
非空判断 |
isAnyBlank(CharSequence... css) isAnyEmpty(CharSequence... css) |
是否存在空字符串(多个中有一个为空,返回true) |
isNoneBlank(CharSequence... css) isNoneEmpty(CharSequence... css) |
是否全部都不是空(都不是空返回true) |
isAllBlank(CharSequence... css) isAllEmpty(CharSequence... css) |
是否全为空(全是空,返回true) |
appendIfMissing(如果字符串没以某个字符串结尾为结尾,就添加该结尾)
例如:String a = "abc";
我们规定 字符串必选以 yes 为结尾,如果abc 没有以 yes 结尾,就自动附加上,如果已经以yes 结尾那么不处理;例如:
官方提供了两个相关方法:
static String |
appendIfMissing(String str, CharSequence suffix, CharSequence... suffixes) Appends the suffix to the end of the string if the string does not already end with any of the suffixes. |
static String |
appendIfMissingIgnoreCase(String str, CharSequence suffix, CharSequence... suffixes) Appends the suffix to the end of the string if the string does not already end, case insensitive, with any of the suffixes. |
解释一下第三个参数,意思就是可以规定多个结尾的字符串,比喻,"yes","no","not" 等,只要其中一个符合便不附加,如果不符合,就把第一个附加到后面,也就是 “yes”
第二个方法是忽略大写匹配
abbreviate(字符串缩略)
该方法可以实现字符串的省略,设置字符长度,后面三个字符显示为 " ... " (如果字符串长度比设置的小,就不会显示了)。如:
官网提供了5个相关方法:
static String |
abbreviate(String str, int maxWidth) Abbreviates a String using ellipses. |
static String |
abbreviate(String str, int offset, int maxWidth) Abbreviates a String using ellipses. |
static String |
abbreviate(String str, String abbrevMarker, int maxWidth) Abbreviates a String using another given String as replacement marker. |
static String |
abbreviate(String str, String abbrevMarker, int offset, int maxWidth) Abbreviates a String using a given replacement marker. |
static String |
abbreviateMiddle(String str, String middle, int length) Abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String. |