您当前的位置: 首页 > 

梁云亮

暂无认证

  • 5浏览

    0关注

    1191博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

判断字符串是否为null、是否为空

梁云亮 发布时间:2021-08-27 11:59:28 ,浏览量:5

org.apache.commons.lang3.StringUtils (掌握) Maven依赖
 
     org.apache.commons
     commons-lang3
     3.9

示例
import org.apache.commons.lang3.StringUtils;

public class DemoTest {
    public static void main(String[] args) {
        String str = null;
        System.out.println(StringUtils.isBlank(str)); //true
        System.out.println(StringUtils.isEmpty(str)); //true

        str = "";
        System.out.println(StringUtils.isBlank(str)); //true
        System.out.println(StringUtils.isEmpty(str)); //true

        str = " ";
        System.out.println(StringUtils.isBlank(str)); //true
        System.out.println(StringUtils.isEmpty(str)); //false
    }
}

记忆:isBlank()在参数为null、""、" "时,值全部为true,在实际项目中使用到该方法比较多。

isEmpty 和 isBlank 的区别?
  • 英语 empty 英 [ˈempti] 美 [ˈempti] adj. 空的;空洞的;空虚的; blank 英 [blæŋk] 美 [blæŋk] adj. 空白的;空的;

  • isEmpty:判断字符串是否为空字符串,只要有任意一个字符(包括空白字符)就不为空。 其源代码:

public static boolean isEmpty(CharSequence cs) {
    return cs == null || cs.length() == 0;
}
  • isBlank:判断字符串是否为空字符串,全部空白字符也为空。

  • 源代码

public static boolean isBlank(CharSequence cs) {
    int strLen;
    if (cs != null && (strLen = cs.length()) != 0) {
        for(int i = 0; i             
关注
打赏
1665023148
查看更多评论
0.0842s