org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String str)。

StringUtils.isEmpty(String str) 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0

System.out.println(StringUtils.isEmpty(null));        //true
System.out.println(StringUtils.isEmpty(""));          //true
System.out.println(StringUtils.isEmpty("   "));       //false
System.out.println(StringUtils.isEmpty("dd"));        //false

StringUtils.isNotEmpty(String str) 等价于 !isEmpty(String str)

StringUtils.isBlank(String str) 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成

System.out.println(StringUtils.isBlank(null));        //true
System.out.println(StringUtils.isBlank(""));          //true
System.out.println(StringUtils.isBlank("   "));       //true
System.out.println(StringUtils.isBlank("dd"));        //false    

StringUtils.isBlank(String str) 等价于 !isBlank(String str)

实例展示

自定义判断方法,实现同样的判断逻辑

    /*** 判断对象是否为null,不允许空白串** @param object    目标对象类型* @return*/public static boolean isNull(Object object){if (null == object) {return true;}if ((object instanceof String)){return "".equals(((String)object).trim());}return false;}/*** 判断对象是否不为null** @param object* @return*/public static boolean isNotNull(Object object){return !isNull(object);}

System.out.println(StringHandler.isNull(null));        //true
System.out.println(StringHandler.isNull(""));          //true
System.out.println(StringHandler.isNull("   "));       //true
System.out.println(StringHandler.isNull("dd"));        //false

转载请注明出处:[http://www.cnblogs.com/dennisit/p/3705374.html]

转载于:https://www.cnblogs.com/renyuanwei/p/9337074.html

StringUtils类中 isEmpty() 与 isBlank()的区别相关推荐

  1. StringUtils 中 isEmpty 和 isBlank 的区别

    在项目的工作学习中经常用到了 apache  commons 中的 StringUtils 的 isBlank 和 isEmpty 来判断字符串是否为空,这个方法都是判断字符串是否为空做判断的,以至于 ...

  2. 工作 3 年的同事不懂 isEmpty 和 isBlank 的区别,我真是醉了。

    新来的同事,干了3年java,代码中 isEmpty 和 isBlank 的区别 都不知道,一顿瞎用.也许你两个都不知道,也许你除了isEmpty/isNotEmpty/isNotBlank/isBl ...

  3. 字符串是否为空(isEmpty和isBlank的区别)

    以前只知道使用没注意具体区别,特此整理总结下. 我们常说的字符串为空,其实就是一个没有字符的空数组.比如: String a = ""; a 就可以称为是一个空字符串.由于 Str ...

  4. java script isblank_java判断一个字符串是否为空,isEmpty和isBlank的区别

    转载于:https://blog.csdn.net/liusa825983081/article/details/78246792 实际应用中,经常会用到判断字符串是否为空的逻辑 比较简单的就是用 S ...

  5. java判断一个字符串是否为空,isEmpty和isBlank的区别

    实际应用中,经常会用到判断字符串是否为空的逻辑 比较简单的就是用 Str != null && Str.length() >0 来判断 其实很多java工具集都是有包装好的接口可 ...

  6. isEmpty和isBlank的区别

    isEmpty和isBlank的区别在于 isEmpty仅仅是判断空和长度为0字符串 isBlank判断的是空,长度为0,空白字符(包括空格,制表符\t,换行符\n,换页符\f,回车\r)组成的字符串 ...

  7. 【转】python类中super()和__init__()的区别

    [转]python类中super()和__init__()的区别 单继承时super()和__init__()实现的功能是类似的 class Base(object):def __init__(sel ...

  8. python super().__init__()参数_python类中super()和__init__()的区别

    单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(self): print 'Base create' class ch ...

  9. StringUtils isEmpty 和 isBlank 的区别 CollectionUtils判空的方法

    本文讨论的 StringUtils 属于package org.apache.commons.lang; 文章目录 字符串判空检查 "" 和 null 的区别 isEmpty(St ...

最新文章

  1. python 列表有几个元素_python怎么向列表中添加多个元素
  2. RACER: Rapid and accurate correction of errors in reads 快速、准确地修正读数中的错误
  3. 【ICLR 2022】在注意力中重新思考Softmax,多个任务达到SOTA
  4. SQL优化:使用explain
  5. 网页css样式中英对照,css中文样式(含中英文对照表).doc
  6. Extension spartacussampledata doesnt specify a path and no scanned extension was matching the name
  7. 终于发现为什么SQL没有释放句柄,原来是保存句柄的变量被覆盖了,丢失了原来的句柄...
  8. dockerfile拉取python3.7镜像
  9. [jQuery基础] jQuery对象 -- 属性操作
  10. 实践单元测试-Using NUnit 大纲
  11. 10条买房错误思维,你中招了几条?
  12. JavaScript-声明变量的关键字
  13. word排版教程(标题设置,自动生成目录)
  14. unity-shader-延迟渲染
  15. Redis总结 其一 概述 安装 类型
  16. CentOS下连VisualSVN服务器时报Key usage violation错误的解决方案
  17. 如皋中学2021高考成绩查询,喜报!如皋八所高中高考成绩公布
  18. Python编程实例-PyQt5 GUI编程-Widgets-QCheckBox
  19. 解决STM32 硬件IIC死锁在BUSY状态的方法讨论
  20. Jemalloc 深入分析 之 配对堆Pairing Heap

热门文章

  1. 《SAP CRM管理与实施指南》一一2.2 SAP CRM基础功能
  2. 《数据中心虚拟化技术权威指南》一2.2 数据中心网络拓扑
  3. libmemcached安装报错
  4. 大山深处,有一所希望学校
  5. what to do preparing for phd
  6. 在做研究的时候很重要的事情
  7. filecoin的思考
  8. 和老师一定要保持沟通
  9. r shiny app learning tutorial a sliderinput
  10. latex表格的整理是需要一定的时间的