org.apache.commons.lang3.StringUtils (掌握)

Maven依赖

 <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version>
</dependency>

示例

import org.apache.commons.lang3.StringUtils;public class DemoTest {public static void main(String[] args) {String str = null;System.out.println(StringUtils.isBlank(str)); //trueSystem.out.println(StringUtils.isEmpty(str)); //truestr = "";System.out.println(StringUtils.isBlank(str)); //trueSystem.out.println(StringUtils.isEmpty(str)); //truestr = " ";System.out.println(StringUtils.isBlank(str)); //trueSystem.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 < strLen; ++i) {// 只要有一个字符不为空白字符就返回 falseif (!Character.isWhitespace(cs.charAt(i))) {return false;}}return true;} else {return true;}
}

使用技巧

我们要判断一个字符串为空,绝大部分情况下 “空白字符” 也要为空的,严谨来说肯定要用 isBlank

org.junit.platform.commons.util.StringUtils (了解)

  • 依赖
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.7.2</version><scope>test</scope></dependency>
  • 测试代码
import org.junit.platform.commons.util.StringUtils;
public class DemoTest {public static void main(String[] args) {String str = null;System.out.println(StringUtils.isBlank(str)); //truestr = "";System.out.println(StringUtils.isBlank(str)); //truestr = " ";System.out.println(StringUtils.isBlank(str)); //true}
}
  • 源代码
 public static boolean isBlank(String str) {return (str == null || str.trim().isEmpty());}

org.springframework.util.StringUtils

public static void main(String[] args) {System.out.println(StringUtils.hasLength(null)); //falseSystem.out.println(StringUtils.hasLength("")); //falseSystem.out.println(StringUtils.hasLength(" "));    //trueSystem.out.println(StringUtils.hasText(null));    //falseSystem.out.println(StringUtils.hasText(""));   //falseSystem.out.println(StringUtils.hasText(" "));  //false
}

判断字符串是否为null、是否为空相关推荐

  1. Extjs 判断对象是非为null或者为空字符串

    Ext.isEmpty(str,[allowEmptyString]) 如果str为 null undefined a zero-length array a zero-length string ( ...

  2. Android 判断字符串是否为空

    TextUtils.isEmpty(str) 可以判断字符串是否为null或者"",当是的时候为true,否的时候为false 程序猿必读 转载于:https://www.cnbl ...

  3. 小程序 js 判断 字符串 为空 null

    判断字符串是否为空 1 2 3 4 5 var strings = ''; if (string.length == 0) { alert('不能为空'); } 判断字符串是否为"空&quo ...

  4. C++ 判断字符串是否为空

    C++ 判断字符串是否为空 有2种方法 1种是用使用empty 2 使用== 与"" 比较 具体的如下: #include <iostream> #include &l ...

  5. ios开发判断字符串为空_【开发常识】这个问题,直接导致年终奖没了……(惨兮兮)...

    关注"Java后端技术全栈" 回复"面试"获取全套面试资料 在项目中,我们基本上都会有个StringUtils工具类,用来处理字符串相关的操作,比如:判空,长度 ...

  6. java - 判断 字符串是否为空

    java - 判断 字符串是否为空 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.1:if(s == null || s.equals(""));方法二: 比较字符 ...

  7. Java 判断字符串是否为空的四种方法、优缺点与注意事项

    以下是Java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s)); 方法二: ...

  8. Java技巧分享:判断字符串是否为空常的三种方法

    很多初学Java的小伙伴肯定都有遇到过需要判断一个字符串是否为空的情况,那么你知道应该如何实现吗?今天小千就来给大家介绍三种不同的方式,同学们看一下哪个适合自己. 判断字符串是否为空有三种常见的方法分 ...

  9. java怎么判断字符串是否为空的几种方法(亲测)

    StringUtils 第一步使用if(StringUtils.isBlank(a))判断字符串a是否为空,为空执行if语句内打印语句,使用StringUtils,需要添加commons-lang-2 ...

  10. java equals 判断空_Java 判断字符串是否为空的三种方法与性能分析

    [java中判断字符串是否为数字的三种方法  1>用JAVA自带的函数 public static boolean isNumeric(String str){   for (int i = s ...

最新文章

  1. -Xms -Xmx -Xmn -Xss -XX:
  2. hydra图形化工具下载_Hydra for Mac 4.0.4 专业的摄影图像工具
  3. c语言函数的三种调用方式是什么?
  4. vue i18n 国际化 使用方法
  5. map、forEach与filter实例详解
  6. js系列教程10-canvas绘图全解
  7. python中str与bytes
  8. Matlab 谢尔宾斯基三角形
  9. Springboot 集成帆软报表(finereport10.0)详细步骤
  10. SyntaxError: Unexpected token u in JSON at position 1
  11. c语言程序设计第三版乌云高娃答案,C语言程序设计教学课件作者第3版乌云高娃补充习题及答案C语言程序设计教学课件作者第3版乌云高娃补充习题及答案第3章补充习题及答案课件.doc...
  12. HDMI转mipiCSI+Audio,东芝,TC358743,视频转换芯片
  13. 域名过期和域名赎回的知识介绍
  14. 网易2016实习研发笔试
  15. iOS-Building for iOS Simulator, but the linked and embedded framework ‘XX.framework‘ was built for
  16. 【异步电路碎碎念1】 —— 到底什么是异步电路
  17. 走进Vue.js 1.0-姜威-专题视频课程
  18. 深入了解光耦,光耦如何连接进电路中?
  19. 摄影测量学-学习笔记整理
  20. 华云数据受邀出席2021年江苏省网络安全发展大会

热门文章

  1. 小生我怕怕私房版OllyDBG全自动下断版
  2. logback分环境配置
  3. Fiddler抓包配置
  4. npp 插件html,Notepad++中常用的插件
  5. java 度分秒转换为度_andriod Java中度转度分秒
  6. 最新免费纯净版PE制作工具V2.1【更新说明】
  7. 【音视频数据数据处理 2】【YUV篇】将YUV420P_I420数据旋转90°-180°-270°-镜像旋转
  8. redis实现CAS
  9. java设计模式之单例模式
  10. Tesseract-OCR4.0识别中文与训练字库实例