本文讨论的 StringUtils 属于package org.apache.commons.lang;

文章目录

  • 字符串判空检查
    • "" 和 null 的区别
    • isEmpty(String str)
    • isBlank(String str)
  • 字符串判空检查
    • "" 和 null 的区别
    • isEmpty(String str)
    • isBlank(String str)
  • 集合判空检查
    • size==0 和 null 的区别
    • isEmpty(Collection coll)

字符串判空检查

要了解字符串判空方法的区别首先要理解对象为空字符串"" 和 null 的区别

“” 和 null 的区别

  • null 是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null。
  • “” 空字符串是一个 String 对象是有地址的,只是内容是空。

关于构造器初始化,在没有显示赋予初值的情况下。默认将数值型赋为 0 , 布尔型是 false,对象引用则是 null。
String 并不是基本数据类型,而是对象所以会被默认的赋予 null。

isEmpty() 和 isBlank() 区别在于 isBlank() 可以多了对于空格的判断,可以根据方法名区别使用 isEmpty() 判断字符串是否为空,而 isBlank() 则是判断字符串是否是空格,空或null

isEmpty(String str)

isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回false

/*** <p>Checks if a String is empty ("") or null.</p>** <pre>* StringUtils.isEmpty(null)      = true* StringUtils.isEmpty("")        = true* StringUtils.isEmpty(" ")       = false* StringUtils.isEmpty("bob")     = false* StringUtils.isEmpty("  bob  ") = false* </pre>** <p>NOTE: This method changed in Lang version 2.0.* It no longer trims the String.* That functionality is available in isBlank().</p>** @param str  the String to check, may be null* @return <code>true</code> if the String is empty or null*/
public static boolean isEmpty(String str) {return str == null || str.length() == 0;
}

isBlank(String str)

isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回true

/*** <p>Checks if a String is whitespace, empty ("") or null.</p>** <pre>* StringUtils.isBlank(null)      = true* StringUtils.isBlank("")        = true* StringUtils.isBlank(" ")       = true* StringUtils.isBlank("bob")     = false* StringUtils.isBlank("  bob  ") = false* </pre>** @param str  the String to check, may be null* @return <code>true</code> if the String is null, empty or whitespace* @since 2.0*/
public static boolean isBlank(String str) {int strLen;if (str == null || (strLen = str.length()) == 0) {return true;}for (int i = 0; i < strLen; i++) {if ((Character.isWhitespace(str.charAt(i)) == false)) {return false;}}return true;
}

本文讨论的 StringUtils 属于package org.apache.commons.lang;

字符串判空检查

要了解字符串判空方法的区别首先要理解对象为空字符串"" 和 null 的区别

“” 和 null 的区别

  • null 是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null。
  • “” 空字符串是一个 String 对象是有地址的,只是内容是空。

关于构造器初始化,在没有显示赋予初值的情况下。默认将数值型赋为 0 , 布尔型是 false,对象引用则是 null。
String 并不是基本数据类型,而是对象所以会被默认的赋予 null。

isEmpty() 和 isBlank() 区别在于 isBlank() 可以多了对于空格的判断,可以根据方法名区别使用 isEmpty() 判断字符串是否为空,而 isBlank() 则是判断字符串是否是空格,空或null

isEmpty(String str)

isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回false

/*** <p>Checks if a String is empty ("") or null.</p>** <pre>* StringUtils.isEmpty(null)      = true* StringUtils.isEmpty("")        = true* StringUtils.isEmpty(" ")       = false* StringUtils.isEmpty("bob")     = false* StringUtils.isEmpty("  bob  ") = false* </pre>** <p>NOTE: This method changed in Lang version 2.0.* It no longer trims the String.* That functionality is available in isBlank().</p>** @param str  the String to check, may be null* @return <code>true</code> if the String is empty or null*/
public static boolean isEmpty(String str) {return str == null || str.length() == 0;
}

isBlank(String str)

isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回true

/*** <p>Checks if a String is whitespace, empty ("") or null.</p>** <pre>* StringUtils.isBlank(null)      = true* StringUtils.isBlank("")        = true* StringUtils.isBlank(" ")       = true* StringUtils.isBlank("bob")     = false* StringUtils.isBlank("  bob  ") = false* </pre>** @param str  the String to check, may be null* @return <code>true</code> if the String is null, empty or whitespace* @since 2.0*/
public static boolean isBlank(String str) {int strLen;if (str == null || (strLen = str.length()) == 0) {return true;}for (int i = 0; i < strLen; i++) {if ((Character.isWhitespace(str.charAt(i)) == false)) {return false;}}return true;
}

本文讨论的 CollectionUtils 属于package org.apache.commons.collections;

集合判空检查

要了解集合判空方法的区别首先要理解对象为size == 0 和 null 的区别

size==0 和 null 的区别

  • null 是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null。
  • size==0 表示集合已经指向一个地址,但是指向的对象中没有元素。

isEmpty() 和 isBlank() 区别在于 isBlank()

isEmpty(Collection coll)

isEmpty(), isNotEmpty() 判段集合是否为null或者不包含任何元素,null 返回true

/*** Null-safe check if the specified collection is empty.* <p>* Null returns true.* * @param coll  the collection to check, may be null* @return true if empty or null* @since Commons Collections 3.2*/
public static boolean isEmpty(Collection coll) {return (coll == null || coll.isEmpty());
}/*** Returns <tt>true</tt> if this collection contains no elements.** @return <tt>true</tt> if this collection contains no elements*/
boolean isEmpty();

StringUtils isEmpty 和 isBlank 的区别 CollectionUtils判空的方法相关推荐

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

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

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

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

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

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

  4. isEmpty和isBlank的区别

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

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

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

  6. Java 判空工具方法大全

    java 判空工具方法大全 前言 一.一般类型的判空 1.String 类型判空 2.包装类型判空 二.类对象判空 1.类对象判空 三.容器类型判空 1.List.Set 判空 2.Map 判空 前言 ...

  7. StringUtils isEmpty 和 isBlank 区别

    判断string类型参数null时,一般 if (xxxxx!= null) {} 然后org.apache.commons.lang.StringUtils 类提供了 String 的常用操作,最为 ...

  8. StringUtils类中 isEmpty() 与 isBlank()的区别

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

  9. StringUtils 中 isEmpty 和 isBlank 的区别

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

最新文章

  1. AD5933不同频率下的转换结果
  2. Java好不好求职?个人能力很重要
  3. 争议?MySQL存储过程与函数,封装,体,完整详细可收藏
  4. ie input兼容 vue_Vue项目与IE11兼容
  5. Centos 6\7下yum安装R
  6. 智能音箱,你在窃听我吗?
  7. mpvue解析富文本mpvue-wxParse
  8. 2021-07-31mysql连接 基本语句
  9. 一个简单的音乐网站项目
  10. 《Dive Into Deeping Learing》学习笔记:深度学习基础
  11. FFT算法的C语言实现
  12. G6实现家族族谱关系图
  13. 大学计算机技术导论,北京邮电大学计算机学院网络技术导论第一章资料.ppt
  14. apple tv 开发_如何在Apple TV上播放计算机中的视频文件
  15. JAVA代码审计——SQL注入靶场审计01
  16. 操作系统 | Mac如何更新word中的域
  17. Fly.Box 企业网盘2.2.1 发布
  18. SD客户信用值(信贷限额、应收款 预收账款、销售值、信贷风险总额、可用余额)
  19. iOS 界面流畅度研究
  20. Qimage颜色显示反色总结

热门文章

  1. 论文阅读笔记:为什么深度神经网络的训练无论多少次迭代永远有效?可能类内分布已经坍缩为一个点,模型已经崩溃为线性分类器
  2. Android7.0 emui主题,基于Android7.0 EMUI5.1系统更流畅
  3. 安卓手机制作简易流程
  4. 深入浅出TensorFlow2函数——tf.reshape
  5. echarts地图 鼠标滚动控制缩放大小比例
  6. 使用 cpca 提取地址所在省份,通过多信息提取地址包含区号并进行表表组合更新
  7. Photoshop CS6 实例之用色彩范围抠图并合成背景
  8. noj 1058 Tom and Jerry
  9. 【xauth: file /home/user/.Xauthority does not exist】用户区产生.Xauthority-n文件解决办法
  10. 6个接私活的网站,你有技术就有钱!