寫在前面: 我是「揚帆向海」,這個暱稱來源於個人名字以及女友的名字。我熱愛技術、熱愛開源、熱愛編程。
技術是開源的、知識是共享的
。
這博客是對本身學習的一點點總結及記錄,若是您對 Java、算法 感興趣,能夠關注個人動態,咱們一塊兒學習。
用知識改變命運,讓咱們的家人過上更好的生活
。java
源碼:
web
public static boolean isEmpty(String str) { // 判斷字符串是否爲空或長度爲0 return str == null || str.length() == 0;
isEmpty 是判斷某個字符串是否爲空,判斷的標準是 str == null || str.length() == 0
算法
測試:編程
public class TestStringUtils { public static void main(String[] args) { System.out.println(StringUtils.isEmpty(null)); // true System.out.println(StringUtils.isEmpty("")); // true System.out.println(StringUtils.isEmpty(" ")); // false System.out.println(StringUtils.isEmpty("\t")); // false System.out.println(StringUtils.isEmpty("揚帆向海")); // false System.out.println(StringUtils.isEmpty(" 揚帆向海 ")); // false } }
源碼:
svg
public static boolean isBlank(String str) { int strLen; // 判斷字符串是否爲空或長度爲是否爲0 if (str != null && (strLen = str.length()) != 0) { // 若是字符串不爲空,且長度不爲0,進行循環遍歷 for(int i = 0; i < strLen; ++i) { // 若是字符串指定位置的值不爲空白字符,返回false;不然返回true if (!Character.isWhitespace(str.charAt(i))) { return false; } } return true; } else { return true; } }
isBlank 是判斷字符串是否爲空或長度爲0 或者是由空白符構成
學習
測試:測試
public class TestStringUtils { public static void main(String[] args) { System.out.println(StringUtils.isBlank(null)); // true System.out.println(StringUtils.isBlank("")); // true System.out.println(StringUtils.isBlank(" ")); // true System.out.println(StringUtils.isBlank("\t")); // true System.out.println(StringUtils.isBlank("揚帆向海")); // false System.out.println(StringUtils.isBlank(" 揚帆向海 ")); // false } }
isEmpty
()方法沒有忽略空格,是以是否爲空和是否存在爲判斷依據;spa
isBlank
()方法增長了字符串爲空格、製表符的判斷。即isBlank()的判斷範圍更大,它在isEmpty()方法的基礎上,包括了空字符的判斷。在實際開發中,isBlank()方法更加經常使用。code
因爲水平有限,本博客不免有不足,懇請各位大佬不吝賜教!xml