主要为了区分一下empty和blank的用法,先看源码:
isEmpty
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public static boolean isEmpty(CharSequence cs) { 2 return cs == null || cs.length() == 0; 3 }
isBlank
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public static boolean isBlank(CharSequence cs) { 2 int strLen; 3 if (cs != null && (strLen = cs.length()) != 0) { 4 for(int i = 0; i < strLen; ++i) { 5 if (!Character.isWhitespace(cs.charAt(i))) { 6 return false; 7 } 8 } 9 10 return true; 11 } else { 12 return true; 13 } 14 }
两者都是判断字符串是否为空的方法,区别是对空格字符的判断。举个例子
对于isEmpty," "不是空,
对于isBlank," "是空。