java判斷字符爲空和非空

if判斷問題

 

    public static void main(String[] args) {
        StringBuffer str = new StringBuffer();
        int i = 0;
        for (int k = 0; k <10; k++) {
            str.append( i+",");
            i++;
        }
        if (StringUtils.isNotBlank(str)){
            System.out.println("判斷非空");
        }
        if (StringUtils.isNotEmpty(str)){
            System.out.println("判斷非空");
        }
        if (StringUtils.isBlank(str)){
            System.out.println("判斷爲空");
        }
    }



控制檯打印

判斷非空
判斷非空

Process finished with exit code 0

底層代碼

   isNotBlank

public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }


public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
}

isNotEmpty

public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
}




public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
}

 

發佈了21 篇原創文章 · 獲贊 8 · 訪問量 6715
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章