关于Spring框架中StringUtils常用/易误用方法解析

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
简介: 关于Spring框架中StringUtils常用/易误用方法解析
Spring 框架给我们提供的StringUtils是我们判断字符串常用的方法,
但是有很多人一直非空判断很混乱,下面做一下总结

方法/步骤


StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false 
//注意在 StringUtils 中空格作非空处理
StringUtils.isEmpty("   ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false


 StringUtils.hasText(null) = false;  
 StringUtils.hasText("") = false;  
 StringUtils.hasText(" ") = false;  
 StringUtils.hasText("12345") = true;  
 StringUtils.hasText(" 12345 ") = true;  


 StringUtils.hasLength(null) = false;  
 StringUtils.hasLength("") = false;  
 StringUtils.hasLength(" ") = true;  
 StringUtils.hasLength("Hello") = true;  
   
  
 //是否包含空白字符  
 StringUtils.containsWhitespace(null)=false;  
 StringUtils.containsWhitespace("")=false;  
 StringUtils.containsWhitespace("a")=false;  
 StringUtils.containsWhitespace("abc")=false;  
 StringUtils.containsWhitespace("abc")=false;  
 StringUtils.containsWhitespace(" ")=true;  
 StringUtils.containsWhitespace(" a")=true;  
 StringUtils.containsWhitespace("abc ")=true;  
 StringUtils.containsWhitespace("a b")=true  
 StringUtils.containsWhitespace("a  b")  
  
 StringUtils.trimWhitespace(null)=null;  
 StringUtils.trimWhitespace("")="";  
 StringUtils.trimWhitespace(" ")="";  
 StringUtils.trimWhitespace("/t")="";  
 StringUtils.trimWhitespace(" a")="a";  
 StringUtils.trimWhitespace("a ")="a";  
 StringUtils.trimWhitespace(" a ")="a";  
 StringUtils.trimWhitespace(" a b ")="a b";  
  
 StringUtils.trimLeadingWhitespace(null)=null;  
 StringUtils.trimLeadingWhitespace("")="";  
 StringUtils.trimLeadingWhitespace(" ")="";  
 StringUtils.trimLeadingWhitespace("/t")="";  
 StringUtils.trimLeadingWhitespace(" a")="a";  
 StringUtils.trimLeadingWhitespace("a ")="a ";  
 StringUtils.trimLeadingWhitespace(" a ")="a ";  
 StringUtils.trimLeadingWhitespace(" a b ")="a b "  
 StringUtils.trimLeadingWhitespace(" a b  c ")="a b  c "  
  
 StringUtils.trimTrailingWhitespace(null)=null;  
 StringUtils.trimTrailingWhitespace(" ")="";  
 StringUtils.trimTrailingWhitespace("/t")="";  
 StringUtils.trimTrailingWhitespace("a ")="a";  
 StringUtils.trimTrailingWhitespace(" a")=" a";  
 StringUtils.trimTrailingWhitespace(" a ")=" a";  
 StringUtils.trimTrailingWhitespace(" a b ")=" a b";  
 StringUtils.trimTrailingWhitespace(" a b  c ")=" a b  c";  
  
  
 StringUtils.trimAllWhitespace("")="";  
 StringUtils.trimAllWhitespace(" ")="";  
 StringUtils.trimAllWhitespace("/t")="";  
 StringUtils.trimAllWhitespace(" a")="a";  
 StringUtils.trimAllWhitespace("a ")="a";  
 StringUtils.trimAllWhitespace(" a ")="a";  
 StringUtils.trimAllWhitespace(" a b ")="ab";  
 StringUtils.trimAllWhitespace(" a b  c "="abc";  

 // 统计一个子字符串在字符串出现的次数  
 StringUtils.countOccurrencesOf(null, null) == 0;  
 StringUtils.countOccurrencesOf("s", null) == 0;  
 StringUtils.countOccurrencesOf(null, "s") == 0;  
 StringUtils.countOccurrencesOf("erowoiueoiur", "WERWER") == 0;  
 StringUtils.countOccurrencesOf("erowoiueoiur", "x")=0;  
 StringUtils.countOccurrencesOf("erowoiueoiur", " ") == 0;  
 StringUtils.countOccurrencesOf("erowoiueoiur", "") == 0;  
 StringUtils.countOccurrencesOf("erowoiueoiur", "e") == 2;  
 StringUtils.countOccurrencesOf("erowoiueoiur", "oi") == 2;  
 StringUtils.countOccurrencesOf("erowoiueoiur", "oiu") == 2;  
 StringUtils.countOccurrencesOf("erowoiueoiur", "oiur") == 1;  
 StringUtils.countOccurrencesOf("erowoiueoiur", "r") == 2;  
  
 //字符串替换  
 String inString = "a6AazAaa77abaa";  
 String oldPattern = "aa";  
 String newPattern = "foo";  
 // Simple replace  
 String s = StringUtils.replace(inString, oldPattern, newPattern);  
 s.equals("a6AazAfoo77abfoo")=true;  
  
 // Non match: no change  
 s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);  
 s.equals(inString)=true  
 s = StringUtils.replace(inString, oldPattern, null);  
 s.equals(inString)=true  
  
 // Null old pattern: should ignore  
 s = StringUtils.replace(inString, null, newPattern);  
        s.equals(inString)=true  
  
 //删除字符串  
 String inString = "The quick brown fox jumped over the lazy dog";  
 String noThe = StringUtils.delete(inString, "the");  
 noThe.equals("The quick brown fox jumped over  lazy dog")=true;  
 String nohe = StringUtils.delete(inString, "he");  
 nohe.equals("T quick brown fox jumped over t lazy dog")=true;  
 String nosp = StringUtils.delete(inString, " ");  
 nosp.equals("Thequickbrownfoxjumpedoverthelazydog")=true;  
 String killEnd = StringUtils.delete(inString, "dog");  
 killEnd.equals("The quick brown fox jumped over the lazy ")=true;  
 String mismatch = StringUtils.delete(inString, "dxxcxcxog");  
  mismatch.equals(inString)=true;  
  
 //删除任何字符  
 //源代码如下  
 //char c = inString.charAt(i);  
 //如果不存在 c 值,则返回 -1  
 //if (charsToDelete.indexOf(c) == -1) {  
 //out.append(c);  
 //}  
  
 String inString = "Able was I ere I saw Elba";  
  
 String res = StringUtils.deleteAny(inString, "I");  
        res.equals("Able was  ere  saw Elba")=true;  
 res = StringUtils.deleteAny(inString, "AeEba!");  
 res.equals("l ws I r I sw l")=true;  
 String mismatch = StringUtils.deleteAny(inString, "#@$#$^");  
 mismatch.equals(inString)=true;  
  
 //源代码如下 return (str != null ? "'" + str + "'" : null);  
 assertEquals("'myString'", StringUtils.quote("myString"));  
 assertEquals("''", StringUtils.quote(""));  
 assertNull(StringUtils.quote(null));  
 //将第一个字符改大写  
 StringUtils.capitalize(Str)  
 //将第一个个字符改小写  
 StringUtils.uncapitalize(str)  
  
 //mypath/myfile.txt" -> "myfile.txt  
 //获取字符串文件名和扩展名  
 StringUtils.getFilename("myfile").equals("myfile")=true;  
 StringUtils.getFilename("mypath/myfile".equals("myfile")=true;  
 StringUtils.getFilename("mypath/myfile".equals("myfile")=true;  
 StringUtils.getFilename("myfile.txt").equals("myfile.txt")=true;  
 StringUtils.getFilename("mypath/myfile.txt").equals("myfile.txt")=true;  
  
 // 获取字符串扩展名,以.分隔  
 StringUtils.getFilenameExtension("myfile")=null;  
 StringUtils.getFilenameExtension("myPath/myfile")=null;  
 StringUtils.getFilenameExtension("myfile.").equals("")=true;  
 StringUtils.getFilenameExtension("myPath/myfile.").equals("")=true;  
 StringUtils.StringUtils.getFilenameExtension("myfile.txt").equals("txt")=true;  
 StringUtils.getFilenameExtension("mypath/myfile.txt").equals("txt")=true;  
  
 //舍去文件名扩展名  
 StringUtils.stripFilenameExtension(null)=true;  
 StringUtils.stripFilenameExtension("").equals("")=true;  
 StringUtils.stripFilenameExtension("myfile").equals("myfile")=true;  
 StringUtils.stripFilenameExtension("mypath/myfile").equals("mypath/myfile")=true;  
 StringUtils.stripFilenameExtension("myfile.").equals("myfile")=true;  
 StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;  
 StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;  
 StringUtils.stripFilenameExtension("myfile.txt").equals("myfile")=true;  
 StringUtils.stripFilenameExtension("mypath/myfile.txt").equals("mypath/myfile")=true

结论

StringUtils.isEmpty() 方法是判断不了空格的,所以要慎重用这个方法
StringUtils.hasText() 可以使用这个方法判断是否有内容,但是使用里面值的时候注意要trim掉左右空格
目录
相关文章
|
4天前
|
XML Java 测试技术
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
这篇文章介绍了Spring5框架的三个新特性:支持@Nullable注解以明确方法返回、参数和属性值可以为空;引入函数式风格的GenericApplicationContext进行对象注册和管理;以及如何整合JUnit5进行单元测试,同时讨论了JUnit4与JUnit5的整合方法,并提出了关于配置文件加载的疑问。
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
|
2天前
|
机器学习/深度学习 人工智能 自然语言处理
【人工智能】Foxmail邮箱在人工智能领域的应用方法及代码解析
Foxmail邮箱作为一款流行的邮件客户端软件,主要用于个人和企业的邮件收发、管理等功能。虽然它与人工智能(AI)技术有着潜在的融合点,但直接关于Foxmail邮箱在人工智能方面的应用代码并不是常规的讨论内容,因为邮箱客户端本身并不直接包含复杂的AI算法或代码。
111 58
|
1天前
|
安全 前端开发 Java
随着企业应用复杂度提升,Java Spring框架以其强大与灵活特性简化开发流程,成为构建高效、可维护应用的理想选择
随着企业应用复杂度提升,Java Spring框架以其强大与灵活特性简化开发流程,成为构建高效、可维护应用的理想选择。依赖注入使对象管理交由Spring容器处理,实现低耦合高内聚;AOP则分离横切关注点如事务管理,增强代码模块化。Spring还提供MVC、Data、Security等模块满足多样需求,并通过Spring Boot简化配置与部署,加速微服务架构构建。掌握这些核心概念与工具,开发者能更从容应对挑战,打造卓越应用。
6 1
|
4天前
|
XML Java 数据格式
Spring Cloud全解析:注册中心之zookeeper注册中心
使用ZooKeeper作为Spring Cloud的注册中心无需单独部署服务器,直接利用ZooKeeper服务端功能。项目通过`spring-cloud-starter-zookeeper-discovery`依赖实现服务注册与发现。配置文件指定连接地址,如`localhost:2181`。启动应用后,服务自动注册到ZooKeeper的`/services`路径下,形成临时节点,包含服务实例信息。
|
3天前
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
4天前
|
XML Java Maven
Spring5入门到实战------16、Spring5新功能 --整合日志框架(Log4j2)
这篇文章是Spring5框架的入门到实战教程,介绍了Spring5的新功能——整合日志框架Log4j2,包括Spring5对日志框架的通用封装、如何在项目中引入Log4j2、编写Log4j2的XML配置文件,并通过测试类展示了如何使用Log4j2进行日志记录。
Spring5入门到实战------16、Spring5新功能 --整合日志框架(Log4j2)
|
4天前
|
Java API Spring
Spring5入门到实战------1、Spring5框架概述、入门案例
这篇文章是Spring5框架的入门教程,概述了Spring框架的核心概念和特点,并通过一个创建普通Java类的案例,详细演示了从下载Spring核心Jar包、创建配置文件、编写测试代码到运行测试结果的完整流程,涵盖了Spring IOC容器的使用和依赖注入的基本用法。
Spring5入门到实战------1、Spring5框架概述、入门案例
|
4天前
|
Java API Spring
Spring5入门到实战------1、Spring5框架概述、入门案例
这篇文章是Spring5框架的入门教程,概述了Spring框架的核心概念和特点,并通过一个创建普通Java类的案例,详细演示了从下载Spring核心Jar包、创建配置文件、编写测试代码到运行测试结果的完整流程,涵盖了Spring IOC容器的使用和依赖注入的基本用法。
|
22天前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
1月前
|
Java 应用服务中间件 开发者
Java面试题:解释Spring Boot的优势及其自动配置原理
Java面试题:解释Spring Boot的优势及其自动配置原理
87 0

推荐镜像

更多