org.springframework.util.StringUtils 使用

我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串。
工具类整理如下:
StringUtils.hasLength(null) = false
StringUtils.hasLength("") = false
StringUtils.hasLength(" ") = true
StringUtils.hasLength("Hello") = true

   StringUtils.hasText(null) = false
   StringUtils.hasText("") = false
   StringUtils.hasText(" ") = false
   StringUtils.hasText("12345") = true
   StringUtils.hasText(" 12345 ") = 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;

下拉式菜单设计——Power BI Power BI导航设计:多级下拉菜单 阅读详情

相关推荐

【数据同化第二期】数据同化中三大理论基础之一(最小方差估计)

【数据同化第二期】深入理解数据同化及经典案例(最小方差估计)

WW、forever的博客 1566

SpringStringUtils 之 hasText()

最近在读Spring源码,发现在读的过程中有很多地方使用 org.springframework.util 包下的 StringUtils 工具类,以下是 这个工具类的源码: 从源码中我们知道这个方法的返回值是 布尔类型的,字符串 不是 null ,并且不为空,而且不能是空白字符,只有这三个条件同时满足时才 返回 true ,其他情况均返回 false 。 拓展: Character.isWhitespace() 方法用于判断指定字符是否为空白字符,空白符包含:空格、tab键、换行符。

No8g攻城狮的博客 1万+

《AI智能体技术 》全套PPT课件2026

《AI智能体技术 》全套PPT课件2026

在Java中从字符串中删除空格

我有一个像这样的字符串: mysz = "name=john age=13 year=2001"; 我想删除字符串中的空格。 我试过trim()但这只删除了整个字符串前后的空格。 我也

p15097962069的博客 2000

StringUtils类isEmpty()、isBlank()、isAnyEmpty() 等判空方法总结,以及trim()字符串去空格

突然想起来最近在开发过程中,经常会遇到字符串不同的判空和去空格情况,最开始总是使用==和equals来结合判空,遇到了StringUtils感觉真是太方便啦!于是想通过源码来区分一下StringUtils类常用的几个方法的使用,以便记忆。 1.StringUtils.isEmpty() /** * <p>Checks if a CharSequence is ...

爱java的小蓝的博客 1万+

Springorg.springframework.util.StringUtils工具类中commaDelimitedListToStringArray的使用

一、源码分析 最近在读 Spring 源码,发现在读的过程中有很多地方使用 org.springframework.util 包下的 StringUtils 工具类,比如这次在源码中看到StringUtils.commaDelimitedListToStringArray() 用法,以下是在 Spring 源码中看到的这种用法: 在查看 org.springframework.util.StringUtils 源码中经过多次调用,最终在方法 delimitedListToStringArray().

No8g攻城狮的博客 1666

import org.springframework.util.StringUtils中的isEmpty()方法已废弃

import org.springframework.util.ObjectUtils这个包下也有一个isEmpty()也可以用于判断一个对象是否为空,尝试一下测试成功,这个方法能用。今天写登录功能需要判断查询出来的用户是否为空,结果用这个方法spring5之后已废弃了。我想着这个方法不能用,还有没有其他的方法,也是发现了一个可以替代的方法。

2303_77534870的博客 154

org.springframework.util.StringUtilsStringUtils工具类

【代码】org.springframework.util.StringUtilsStringUtils工具类。

m0_59690068的博客 1581

Spring工具类org.springframework.util.StringUtils工具类

Spring工具类StringUtils常用方法 org.springframework.util.StringUtils StringUtils常用方法 描述 booleanisEmpty(Object str) 判断字符串是否为空,如果为nul`或者`""则返回true,否则返回false booleanhasLength(CharSequence str) 判断字符串是否有长度,字符串不等于null且长度大于0,则为true,否则返回false booleanhasText(C

Java小皮孩 5812

org.springframework.util.StringUtils使用

我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串。  工具类整理如下:    StringUtils.hasLength(null) = false    StringUtils.hasLength("") = false    StringUtils.hasLen

淘技术 4972

org.springframework.util.StringUtils使用

我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串。  工具类整理如下:    StringUtils.hasLength(null) = false    StringUtils.hasLength("") = false    StringUtils.hasLen

wh351531104的专栏 8030

Spring的工具类StringUtils使用

org.springframework.util.StringUtils 我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串。 工具类整理如下: StringUtils.hasLength(null) = false StringUtils.hasLength("") = f...

文文的博客 1487

java 去掉 u00a0,String的trim()方法不能去除的空格(ASCII码160)

JAVA 去除前后空格String trim 方法去除String space = "这是空格 ";//content 的字符串是: "这是空格 " (肉眼看,前后都有空格),//这里是为了达到我们的效果,让看起来就跟普通空格一样String content = StringEscapeUtils.unescapeHtml4(space);//trim1 的结果是" 这是空格" (最前面还是...

weixin_33450988的博客 1206

【编程基础】Java去除字符串中多余空格(案例详解)

【辰兮要努力】:hello你好我是辰兮,很高兴你能来阅读,昵称是希望自己能不断精进,向着优秀程序员前行! 博客来源于项目以及编程中遇到的问题总结,偶尔会有读书分享,我会陆续更新Java前端、后台、数据库、项目案例等相关知识点总结,感谢你的阅读和关注,希望我的博客能帮助到更多的人,分享获取新知,大家一起进步! 吾等采石之人,应怀大教堂之心,愿大家奔赴在各自的热爱里… 文章目录一、Java二、JavaScript 一、Java 业务场景:在Java代码中偶尔遇到需要去除字符串中的空格相关方法 Java.

末尾卡片和博主交流学习! 4029

(二十九)、Java字符串中去除空格

1.方法分类 str.trim(); //去掉首尾空格 str.replace(" ",""); //去除所有空格,包括首尾、中间 str.replaceAll(" ", ""); //去掉所有空格,包括首尾、中间 str.replaceAll(" +",""); //去掉所有空格,包括首尾、中间 str.replaceAll("\\s*", ""); //可以替换大部分空白字符,...

weixin_30894583的博客 162

StringUtils里的isEmpty和hasText方法的理解与使用

工具判断方法:一种是包下的;一种是包下的。

qq_41650485的博客 1403

StringUtils.hasTex的作用

StringUtilsspring框架(包org.springframework.util.StringUtils)为我们提供的一个工具类,我们可以用类名调用hasText方法,调用方式为 StringUtils.hasText(user.getUserName()) 如果user.getUserName为空的话,该方法返回false; 如果user.getUserName不为空的话,该方法返...

travel7623的博客 1380

apache的replace,trim方法 StringUtils.replace(),StringUtils.trimWhitespace() java原生

apache的replace,trim方法 StringUtils.replace(),StringUtils.trimWhitespace() java原生的replace,trim方法之间性能的巨大差别。  Java代码   public class App    {       final static String teString = "    55555559999...

newXing 2247
上一篇: 如何联合使用Union和Order by
下一篇: Oracle中start with...connect by prior子句用法
callan
博客等级 码龄18年 1粉丝 42原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值