其实对于字符串判空来说不是什么有难度的操作,甚至是低难度的,只是对于’\u0000’这个字符有的人可能还是比较陌生,而它就是这篇博客的重点!
我们先看一下’\u0000’打印出来的效果是什么样的吧,如下:
- 打印代码
public static void main(String[] args) {
System.out.println("----\u0000----");
}
- 打印结果
Connected to the target VM, address: '127.0.0.1:51581', transport: 'socket'
---- ----
Disconnected from the target VM, address: '127.0.0.1:51581', transport: 'socket'
Process finished with exit code 0
从上面的打印结果可知’\u0000’从表现形式来看确实是个"空格",但是它确实是不为空的,因为它既不是null也不是’ ‘空格字符(哈哈哈,有点绕),那我们应该怎么样处理包含’\u0000’字符的判空场景呢?
- 测试代码
public class StringUtils {
public static boolean isNull(String str) {
return str == null;
}
public static boolean isEmpty(String str) {
return isNull(str) || "".equals(str);
}
public static boolean isBlank(String str) {
if (isEmpty(str)) {
return true;
}
int strLen = str.length();
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isAbsoluteBlank(String str) {
if (isEmpty(str)) {
return true;
}
int strLen = str.length();
char c;
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(c = str.charAt(i)) && c != '\u0000') {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println("===================== test isNull =====================");
System.out.println("null is " + isNull(null));
System.out.println("\"\" is " + isNull(""));
System.out.println("===================== test isEmpty =====================");
System.out.println("null is " + isEmpty(null));
System.out.println("\"\" is " + isEmpty(""));
System.out.println("\" \" is " + isEmpty(" "));
System.out.println("===================== test isBlank =====================");
System.out.println("null is " + isBlank(null));
System.out.println("\"\" is " + isBlank(""));
System.out.println("\" \" is " + isBlank(" "));
System.out.println("\\u0000 is " + isBlank("\u0000"));
System.out.println("===================== test isAbsoluteBlank =====================");
System.out.println("null is " + isAbsoluteBlank(null));
System.out.println("\"\" is " + isAbsoluteBlank(""));
System.out.println("\" \" is " + isAbsoluteBlank(" "));
System.out.println("\\u0000 is " + isAbsoluteBlank("\u0000"));
}
}
- 打印结果
Connected to the target VM, address: '127.0.0.1:51742', transport: 'socket'
===================== test isNull =====================
null is true
"" is false
===================== test isEmpty =====================
null is true
"" is true
" " is false
===================== test isBlank =====================
null is true
"" is true
" " is true
\u0000 is false
===================== test isAbsoluteBlank =====================
null is true
"" is true
" " is true
\u0000 is true
Disconnected from the target VM, address: '127.0.0.1:51742', transport: 'socket'
通过上面的代码我们发现isAbsoluteBlank 方法是满足包含’\u0000’字符的判空场景的!
这样说来,那我们以后是不是对于字符串的判空都要改成isAbsoluteBlank 方法一样的逻辑了呢?
其实不是的,字符串判空在我看来是个很看具体业务逻辑的操作,对于同样的ugc来说,昵称我们可以让用户使用’\u0000’,这样可以在app或者网页上显示出类似于没有昵称的效果,是不是很炫?但是对于评论来说,我觉得就不应该允许使用’\u0000’了(其实也可以对输入的’\u0000’字符进行转义处理),其实也就等效于用户什么都没评论,这种评论即使发布成功,又让大家看什么呢?
好了,说到这里都是一家之言,希望大家有什么不一样见解的可以在评论区留言,可以试一试’\u0000’看看(偷笑)!