在java中如何正確的判斷字符串是否爲"空"

其實對於字符串判空來說不是什麼有難度的操作,甚至是低難度的,只是對於’\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’看看(偷笑)!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章