前言
估计很多朋友跟我一样,平时也不会特别去注意究竟用isBlank
还是isEmpty
去判断空字符串,但是大部分场景优先使用isBlank
就对了。
- isEmpty是否为空,只有当
==null
或者==""
才为空 - isBlank是否为真空,
==null
和==""
以及各种长度的空格==" "
都为空
而且除了isEmpty/isNotEmpty/isNotBlank/isBlank外,其实还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank让我们一起来研究一下org.apache.commons.lang3.StringUtils
这个工具类吧。
isBank()
是否为真空值(空格或者空值)
- StringUtils.isBlank(null) = true
- StringUtils.isBlank("") = true
- StringUtils.isBlank(" ") = true
- StringUtils.isBlank(" ") = true
- StringUtils.isBlank("moshow") = false
- StringUtils.isBlank(" moshow ") = false
/**
* <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is null, empty or whitespace
* @since 2.0
* @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
*/
public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(cs.charAt(i)) == false) {
return false;
}
}
return true;
}
StringUtils.isNotBlank()
是否真的不为空,不是空格或者空值 ,相当于!isBlank();
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}
StringUtils.isAnyBlank()
是否包含任何真空值(包含空格或空值)
StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank(null, "foo") = true
StringUtils.isAnyBlank(null, " ") = true
StringUtils.isAnyBlank("", " ") = true
/**
* <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p>
* @param css the CharSequences to check, may be null or empty
* @return {@code true} if any of the CharSequences are blank or null or whitespace only
* @since 3.2
*/
public static boolean isAnyBlank(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
}
for (final CharSequence cs : css){
if (isBlank(cs)) {
return true;
}
}
return false;
}
StringUtils.isNoneBlank()
是否没有空值或空格
StringUtils.isNoneBlank(null) = false
StringUtils.isNoneBlank(null, "foo") = false
StringUtils.isNoneBlank(null, " ") = false
StringUtils.isNoneBlank("", " ") = false
StringUtils.isNoneBlank("moshow", " moshow ") = true
关于其他方法其实都是以此类推,知道这几个,就可以类推对应的is*Empty等等鞥,这里就不做重复展示。
/**
* <p>Checks if none of the CharSequences are blank ("") or null and whitespace only..</p>
* @param css the CharSequences to check, may be null or empty
* @return {@code true} if none of the CharSequences are blank or null or whitespace only
* @since 3.2
*/
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}
StringUtils的其他方法
官方文档中有提及,这里做一个简单整理和翻译。有些方法确实很好用,但是平时并不会去用。
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
方法 (整理 by https://zhengkai.blog.csdn.net/ ) | 说明EN | 说明CN |
---|---|---|
IsEmpty/IsBlank | checks if a String contains text | 判断是否包含文本 |
Trim/Strip | removes leading and trailing whitespace | 删除多余空格 |
Equals/Compare | compares two strings in a null | safe manner |
startsWith /endsWith | check if a String starts/ends with a prefix | 判断是什么开头/结尾 |
IndexOf/LastIndexOf/Contains | contain a string and return the index | 判断字符串在string中的位置,判断是否包含 |
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut | index of any of a set of Strings | 如果包含某几个字符串就返回 |
ContainsOnly/ContainsNone/ContainsAny | checks if String contains only/none/any of these characters | |
Substring/Left/Right/Mid/SubstringBefore/SubstringAfter/SubstringBetween | safe substring extractions | 截取字符串方法 |
Split/Join | splits a String into an array of substrings and vice versa | 切割或者拼接字符串 |
Remove/Delete | removes part of a String | 移除 |
Replace/Overlay | Searches a String and replaces one String with another | 替换 |
Chomp/Chop | removes the last part of a String | 删除字符串的最后一部分 |
AppendIfMissing/PrependIfMissing | appends a suffix to the start/end of the String if not present | 前缀后缀 |
LeftPad/RightPad/Center/Repeat | pads a String | 填充字符串 |
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize | changes the case of a String | 大写/小写/交换大小写/大写/取消大写 |
CountMatches | counts the number of occurrences of one String in another | 统计匹配 |
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable | checks the characters in a String | 是否字母/数字/空格/Ascii |
DefaultString | protects against a null input String | 防止空输入字符串 |
Rotate | rotate (circular shift) a String | 旋转(循环移位)一个字符串 |
Reverse/ReverseDelimited | reverses a String | 反转字符串 |
Abbreviate | abbreviates a string using ellipses or another given String | 使用省略号或另一个给定的字符串缩写字符串 |
Difference | compares Strings and reports on their differences | 比较字符串并报告它们的差异 |
LevenshteinDistance | the number of changes needed to change one String into another | 将一个字符串更改为另一个字符串所需的更改次数 |