js 非空判断
- 是否为 null
- 是否为 ""
- 是否为空字符串(引号中间有空格) 如: " "。
- 制表符、换行符、换页符和回车
一. 字符串
1. if(str == null || str == "") 非空 if(str != null && str != "")
2. if(str == null || str.isEmpty()) if(str != null && !str.isEmpty())
3.if (str == null || "".equals(str.trim())) if (str != null && !"".equals(str.trim()))
4.if(str == null || str.length()<=0) if(str != null && str.length()>0)
5.if(str == null || "".equals(str)) if(str != null && !"".equals(str))
6.if(StringUtils.isBlank(str)) if(StringUtils.isNotBlank(str)) import org.apache.commons.lang3.StringUtils;判断的是str.length(),相当于4
二.数组
1.arr==null || (arr!=null &&arr.length==0) 非空 arr!=null || (arr==null &&arr.length!=0)
三.List集合
1.if(list == null || list.isEmpty()) 非空 if(list != null && !list.isEmpty())
2.if(list == null || list.size() == 0) if(list != null && list.size() > 0)
3.if(list == null || StringUtils.isEmpty(list)) if(list != null && !StringUtils.isEmpty(list))
4.if (CollectionUtils.isEmpty(list)) if (CollectionUtils.isNotEmpty(list)) 等同于2
四.Map
1.if (MapUtils.isEmpty(map)) 非空 if (MapUtils.isNotEmpty(map)) 等同于2
2.if(map== null || map.size() == 0) if(map!= null && map.size() > 0)
3.if(map== null || StringUtils.isEmpty(map)) if(map!= null && !StringUtils.isEmpty(map))
null和isEmpty()的区别
- 这就相当于去商店买东西
- null 首先判断是否有商店(new ArrayList();)
- isEmpty()没有判断商店是否存在,而是判断商店是否有东西,如果连商店都没有,何来的的东西可卖(list.add(商品))
最新文章
- [AlwaysOn Availability Groups]健康模型 Part 1——概述
- vlc 在ie11 中的addEventListener事件不触发
- CentOS下的Memcache安装步骤(Linux+Nginx+PHP+Memcached)
- IIS+WebMatrix 做 PHP 遇到的问题总结
- assets
- UIImageView之我的动画为什么停了?UIImageView, highLighted,animationImages
- python基础-软件目录开发规范
- [bilibili]弹幕屏蔽列表
- windows平台MySQL密码设置与破解
- java基础 ---- 练习for循环
- 【转载】java对象和byte数组互转,直接拿去用
- java与xml转换 -- XStreamAlias
- .3-浅析express源码之applicaiton模块(2)-app.render
- MySQL 查询优化简记
- sudo: add-apt-repository: command not found
- 【转载】Java动态代理之JDK实现和CGlib实现(简单易懂)
- Maven新建一个Spring MVC项目
- odps编写UDF的实现
- (beta冲刺5/7)
- jrtplib源码分析 第一篇 jthread的编译与分析