Spring的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
Spring的StringUtils工具类的更多相关文章
- spring util包 StringUtils工具类中的isEmpty() 方法解析
今天在公司看到同事写的代码,无意发现在判断字符串类型时,使用的是StringUtils工具类中的isEmpty()去判断如下所示 @RequestMapping(value = "/pub/ ...
- 简单了解Spring中常用工具类_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...
- Spring 的优秀工具类盘点
文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...
- Spring 的优秀工具类盘点第 2 部分
特殊字符转义 由于 Web 应用程序需要联合使用到多种语言,每种语言都包含一些特殊的字符,对于动态语言或标签式的语言而言,如果需要动态构造语言的内容时,一个我们经常会碰到的问题就是特殊字符转义的问题. ...
- StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取 StringUtils ...
- StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方 ...
- StringUtils工具类常用方法
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- Spring获取bean工具类,可用于在线程里面获取bean
Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...
随机推荐
- Linux中命令行编译java接口总是提示找不到符号的疑难杂症的解决
今天学习java的接口,在linux的命令行下写代码练练手吧,啪啪啪一顿猛敲,写了一个接口UsbInserface,UDisk继承UsbInterface,写完了那就编译到bin目录呗. 当时写程序的 ...
- C# DBNULL与NULL之间的区别【转】
null是.net中无效的对象引用.DBNull是一个类.DBNull.Value是它唯一的实例.它指数据库中数据为空(<NULL>)时,在.net中的值.null表示一个对象的指向无效, ...
- 关于 submit 按钮的 onclick 验证事件,第一次验证失败,第二次 submit 按钮失效的原因解析
今天在做一个生成订单的简单验证页面 主要功能:实现点击提交按钮后,先在页面上用JavaScript验证信息是否输入完全,不完全不允许提交,待输入完全后,才可以提交 页面:如下页面,可以看到,当文本框有 ...
- hdu 1022
// hdu1022 这题算是我做的第一道栈的题目,之前看过栈的一些内容,做这道题的时候,可以模拟出过程,但是具体的代码不会写...所以决定练习一些栈和队列的问题,大概思路就是有三个数组和一个栈,先把 ...
- 分析器错误消息: 未能加载类型“WebApplication._Default”
1.新建一个空白解决方案2.新闻一个Web Application项目 默认就有Default.aspx 直接调试的时候出现-------------------------------------- ...
- C#Css/Js静态文件压缩--Yui.Compressor.Net
一.Asp.Net 自带静态文件压缩工具包 Microsoft.AspNet.Web.Optimization http://www.nuget.org/packages/Microsoft.AspN ...
- Lesson 6: Exploring the World of Typefaces
Lesson 6: Exploring the World of Typefaces 这课提到的字体都是 英文 的. Article 1: More Google Web Fonts That Don ...
- JavaScript 之 使用 XMLHttpRequest 上传文件
<div id="div1"> <input type="file" id="uploadfile" style=&quo ...
- vs2008下使用libcurl
网上找了半天,总算找到一个比较好用的C++ 网络库,老实说,完全用Socket操作网络对于需要开发网络应用程序的人员来说还是很蛋疼很繁琐的.好在有这么一个给力的库.这个库的介绍可以自己百度一下,就我所 ...
- 直接拨号、将电话号码传入拨号程序、调用拨号程序、调用系统浏览器浏览网页、调用系统程序查看联系人、显示系统设置界面和显示Wi-Fi设置界面代码
直接拨号.将电话号码传入拨号程序.调用拨号程序.调用系统浏览器浏览网页.调用系统程序查看联系人.显示系统设置界面和显示Wi-Fi设置界面代码 拨打号码的代码如下: Intent callIntent= ...