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

简介: 关于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掉左右空格
目录
相关文章
|
3月前
|
数据采集 人工智能 Java
1天消化完Spring全家桶文档!DevDocs:一键深度解析开发文档,自动发现子URL并建立图谱
DevDocs是一款基于智能爬虫技术的开源工具,支持1-5层深度网站结构解析,能将技术文档处理时间从数周缩短至几小时,并提供Markdown/JSON格式输出与AI工具无缝集成。
142 1
1天消化完Spring全家桶文档!DevDocs:一键深度解析开发文档,自动发现子URL并建立图谱
|
3月前
|
安全 Java API
深入解析 Spring Security 配置中的 CSRF 启用与 requestMatchers 报错问题
本文深入解析了Spring Security配置中CSRF启用与`requestMatchers`报错的常见问题。针对CSRF,指出默认已启用,无需调用`enable()`,只需移除`disable()`即可恢复。对于`requestMatchers`多路径匹配报错,分析了Spring Security 6.x中方法签名的变化,并提供了三种解决方案:分次调用、自定义匹配器及降级使用`antMatchers()`。最后提醒开发者关注版本兼容性,确保升级平稳过渡。
336 2
|
3月前
|
前端开发 Java 物联网
智慧班牌源码,采用Java + Spring Boot后端框架,搭配Vue2前端技术,支持SaaS云部署
智慧班牌系统是一款基于信息化与物联网技术的校园管理工具,集成电子屏显示、人脸识别及数据交互功能,实现班级信息展示、智能考勤与家校互通。系统采用Java + Spring Boot后端框架,搭配Vue2前端技术,支持SaaS云部署与私有化定制。核心功能涵盖信息发布、考勤管理、教务处理及数据分析,助力校园文化建设与教学优化。其综合性和可扩展性有效打破数据孤岛,提升交互体验并降低管理成本,适用于日常教学、考试管理和应急场景,为智慧校园建设提供全面解决方案。
296 70
|
2月前
|
监控 Java 调度
SpringBoot中@Scheduled和Quartz的区别是什么?分布式定时任务框架选型实战
本文对比分析了SpringBoot中的`@Scheduled`与Quartz定时任务框架。`@Scheduled`轻量易用,适合单机简单场景,但存在多实例重复执行、无持久化等缺陷;Quartz功能强大,支持分布式调度、任务持久化、动态调整和失败重试,适用于复杂企业级需求。文章通过特性对比、代码示例及常见问题解答,帮助开发者理解两者差异,合理选择方案。记住口诀:单机简单用注解,多节点上Quartz;若是任务要可靠,持久化配置不能少。
214 4
|
2月前
|
安全 Java API
Spring Boot 功能模块全解析:构建现代Java应用的技术图谱
Spring Boot不是一个单一的工具,而是一个由众多功能模块组成的生态系统。这些模块可以根据应用需求灵活组合,构建从简单的REST API到复杂的微服务系统,再到现代的AI驱动应用。
307 9
|
2月前
|
Java 开发者 Spring
Spring框架 - 深度揭秘Spring框架的基础架构与工作原理
所以,当你进入这个Spring的世界,看似一片混乱,但细看之下,你会发现这里有个牢固的结构支撑,一切皆有可能。不论你要建设的是一座宏大的城堡,还是个小巧的花园,只要你的工具箱里有Spring,你就能轻松搞定。
103 9
|
3月前
|
Java Spring
Spring框架的学习与应用
总的来说,Spring框架是Java开发中的一把强大的工具。通过理解其核心概念,通过实践来学习和掌握,你可以充分利用Spring框架的强大功能,提高你的开发效率和代码质量。
102 20
|
3月前
|
Java 关系型数据库 MySQL
深入解析 @Transactional——Spring 事务管理的核心
本文深入解析了 Spring Boot 中 `@Transactional` 的工作机制、常见陷阱及最佳实践。作为事务管理的核心注解,`@Transactional` 确保数据库操作的原子性,避免数据不一致问题。文章通过示例讲解了其基本用法、默认回滚规则(仅未捕获的运行时异常触发回滚)、因 `try-catch` 或方法访问修饰符不当导致失效的情况,以及数据库引擎对事务的支持要求。最后总结了使用 `@Transactional` 的五大最佳实践,帮助开发者规避常见问题,提升项目稳定性与可靠性。
363 12
|
3月前
|
安全 Java 数据安全/隐私保护
Spring Security: 深入解析 AuthenticationSuccessHandler
本文深入解析了 Spring Security 中的 `AuthenticationSuccessHandler` 接口,它用于处理用户认证成功后的逻辑。通过实现该接口,开发者可自定义页面跳转、日志记录等功能。文章详细讲解了接口方法参数及使用场景,并提供了一个根据用户角色动态跳转页面的示例。结合 Spring Security 配置,展示了如何注册自定义的成功处理器,帮助开发者灵活应对认证后的多样化需求。
104 2

热门文章

最新文章

推荐镜像

更多
  • DNS
  • AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等

    登录插画

    登录以查看您的控制台资源

    管理云资源
    状态一览
    快捷访问