关于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掉左右空格
目录
相关文章
|
缓存 安全 Java
深入解析HTTP请求方法:Spring Boot实战与最佳实践
这篇博客结合了HTTP规范、Spring Boot实现和实际工程经验,通过代码示例、对比表格和架构图等方式,系统性地讲解了不同HTTP方法的应用场景和最佳实践。
1219 5
|
Java Spring 容器
两种Spring Boot 项目启动自动执行方法的实现方式
在Spring Boot项目启动后执行特定代码的实际应用场景中,可通过实现`ApplicationRunner`或`CommandLineRunner`接口完成初始化操作,如系统常量或配置加载。两者均支持通过`@Order`注解控制执行顺序,值越小优先级越高。区别在于参数接收方式:`CommandLineRunner`使用字符串数组,而`ApplicationRunner`采用`ApplicationArguments`对象。注意,`@Order`仅影响Bean执行顺序,不影响加载顺序。
1082 2
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `<appender>` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `<logger>` 和 `<root>` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
3297 1
|
安全 IDE Java
重学Java基础篇—Java Object类常用方法深度解析
Java中,Object类作为所有类的超类,提供了多个核心方法以支持对象的基本行为。其中,`toString()`用于对象的字符串表示,重写时应包含关键信息;`equals()`与`hashCode()`需成对重写,确保对象等价判断的一致性;`getClass()`用于运行时类型识别;`clone()`实现对象复制,需区分浅拷贝与深拷贝;`wait()/notify()`支持线程协作。此外,`finalize()`已过时,建议使用更安全的资源管理方式。合理运用这些方法,并遵循最佳实践,可提升代码质量与健壮性。
565 1
|
JSON 监控 网络协议
Bilibili直播信息流:连接方法与数据解析
本文详细介绍了自行实现B站直播WebSocket连接的完整流程。解析了基于WebSocket的应用层协议结构,涵盖认证包构建、心跳机制维护及数据包解析步骤,为开发者定制直播数据监控提供了完整技术方案。
2604 9
|
传感器 监控 Java
Java代码结构解析:类、方法、主函数(1分钟解剖室)
### Java代码结构简介 掌握Java代码结构如同拥有程序世界的建筑蓝图,类、方法和主函数构成“黄金三角”。类是独立的容器,承载成员变量和方法;方法实现特定功能,参数控制输入环境;主函数是程序入口。常见错误包括类名与文件名不匹配、忘记static修饰符和花括号未闭合。通过实战案例学习电商系统、游戏角色控制和物联网设备监控,理解类的作用、方法类型和主函数任务,避免典型错误,逐步提升编程能力。 **脑图速记法**:类如太空站,方法即舱段;main是发射台,static不能换;文件名对仗,括号要成双;参数是坐标,void不返航。
659 5
|
人工智能 自然语言处理 Java
Spring 集成 DeepSeek 的 3大方法(史上最全)
DeepSeek 的 API 接口和 OpenAI 是兼容的。我们可以自定义 http client,按照 OpenAI 的rest 接口格式,去访问 DeepSeek。自定义 Client 集成DeepSeek ,可以通过以下步骤实现。步骤 1:准备工作访问 DeepSeek 的开发者平台,注册并获取 API 密钥。DeepSeek 提供了与 OpenAI 兼容的 API 端点(例如),确保你已获取正确的 API 地址。
Spring 集成 DeepSeek 的 3大方法(史上最全)
|
编解码 缓存 Prometheus
「ximagine」业余爱好者的非专业显示器测试流程规范,同时也是本账号输出内容的数据来源!如何测试显示器?荒岛整理总结出多种测试方法和注意事项,以及粗浅的原理解析!
本期内容为「ximagine」频道《显示器测试流程》的规范及标准,我们主要使用Calman、DisplayCAL、i1Profiler等软件及CA410、Spyder X、i1Pro 2等设备,是我们目前制作内容数据的重要来源,我们深知所做的仍是比较表面的活儿,和工程师、科研人员相比有着不小的差距,测试并不复杂,但是相当繁琐,收集整理测试无不花费大量时间精力,内容不完善或者有错误的地方,希望大佬指出我们好改进!
1447 16
「ximagine」业余爱好者的非专业显示器测试流程规范,同时也是本账号输出内容的数据来源!如何测试显示器?荒岛整理总结出多种测试方法和注意事项,以及粗浅的原理解析!
|
传感器 监控 安全
智慧工地云平台的技术架构解析:微服务+Spring Cloud如何支撑海量数据?
慧工地解决方案依托AI、物联网和BIM技术,实现对施工现场的全方位、立体化管理。通过规范施工、减少安全隐患、节省人力、降低运营成本,提升工地管理的安全性、效率和精益度。该方案适用于大型建筑、基础设施、房地产开发等场景,具备微服务架构、大数据与AI分析、物联网设备联网、多端协同等创新点,推动建筑行业向数字化、智能化转型。未来将融合5G、区块链等技术,助力智慧城市建设。
880 1
|
XML Java 开发者
Spring底层架构核心概念解析
理解 Spring 框架的核心概念对于开发和维护 Spring 应用程序至关重要。IOC 和 AOP 是其两个关键特性,通过依赖注入和面向切面编程实现了高效的模块化和松耦合设计。Spring 容器管理着 Beans 的生命周期和配置,而核心模块为各种应用场景提供了丰富的功能支持。通过全面掌握这些核心概念,开发者可以更加高效地利用 Spring 框架开发企业级应用。
529 18

热门文章

最新文章

推荐镜像

更多
  • DNS