Spring的StringUtils工具类

摘要:
10StringUtils.hasText(“12345”)=真;14StringUtils.containsWhitespace(“”)=false;15StringUtils.containsWhitespace(“a”)=false;17StringUtils.containsWhitespace(“abc”)=false;

本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除。

原文网址:《Spring的StringUtils工具类》

org.springframework.util.StringUtils

我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串。
工具类整理如下:
  1 StringUtils.hasLength(null) = false;  
  2  StringUtils.hasLength("") = false;  
  3  StringUtils.hasLength(" ") = true;  
  4  StringUtils.hasLength("Hello") = true;  
  5    
  6  StringUtils.hasText(null) = false;  
  7  StringUtils.hasText("") = false;  
  8  StringUtils.hasText(" ") = false;  
  9  StringUtils.hasText("12345") = true;  
 10  StringUtils.hasText(" 12345 ") = true;  
 11   
 12  //是否包含空白字符  
 13  StringUtils.containsWhitespace(null)=false;  
 14  StringUtils.containsWhitespace("")=false;  
 15  StringUtils.containsWhitespace("a")=false;  
 16  StringUtils.containsWhitespace("abc")=false;  
 17  StringUtils.containsWhitespace("abc")=false;  
 18  StringUtils.containsWhitespace(" ")=true;  
 19  StringUtils.containsWhitespace(" a")=true;  
 20  StringUtils.containsWhitespace("abc ")=true;  
 21  StringUtils.containsWhitespace("a b")=true  
 22  StringUtils.containsWhitespace("a  b")  
 23   
 24  StringUtils.trimWhitespace(null)=null;  
 25  StringUtils.trimWhitespace("")="";  
 26  StringUtils.trimWhitespace(" ")="";  
 27  StringUtils.trimWhitespace("/t")="";  
 28  StringUtils.trimWhitespace(" a")="a";  
 29  StringUtils.trimWhitespace("a ")="a";  
 30  StringUtils.trimWhitespace(" a ")="a";  
 31  StringUtils.trimWhitespace(" a b ")="a b";  
 32   
 33  StringUtils.trimLeadingWhitespace(null)=null;  
 34  StringUtils.trimLeadingWhitespace("")="";  
 35  StringUtils.trimLeadingWhitespace(" ")="";  
 36  StringUtils.trimLeadingWhitespace("/t")="";  
 37  StringUtils.trimLeadingWhitespace(" a")="a";  
 38  StringUtils.trimLeadingWhitespace("a ")="a ";  
 39  StringUtils.trimLeadingWhitespace(" a ")="a ";  
 40  StringUtils.trimLeadingWhitespace(" a b ")="a b "  
 41  StringUtils.trimLeadingWhitespace(" a b  c ")="a b  c "  
 42   
 43  StringUtils.trimTrailingWhitespace(null)=null;  
 44  StringUtils.trimTrailingWhitespace(" ")="";  
 45  StringUtils.trimTrailingWhitespace("/t")="";  
 46  StringUtils.trimTrailingWhitespace("a ")="a";  
 47  StringUtils.trimTrailingWhitespace(" a")=" a";  
 48  StringUtils.trimTrailingWhitespace(" a ")=" a";  
 49  StringUtils.trimTrailingWhitespace(" a b ")=" a b";  
 50  StringUtils.trimTrailingWhitespace(" a b  c ")=" a b  c";  
 51   
 52   
 53  StringUtils.trimAllWhitespace("")="";  
 54  StringUtils.trimAllWhitespace(" ")="";  
 55  StringUtils.trimAllWhitespace("/t")="";  
 56  StringUtils.trimAllWhitespace(" a")="a";  
 57  StringUtils.trimAllWhitespace("a ")="a";  
 58  StringUtils.trimAllWhitespace(" a ")="a";  
 59  StringUtils.trimAllWhitespace(" a b ")="ab";  
 60  StringUtils.trimAllWhitespace(" a b  c "="abc";  
 61  // 统计一个子字符串在字符串出现的次数  
 62  StringUtils.countOccurrencesOf(null, null) == 0;  
 63  StringUtils.countOccurrencesOf("s", null) == 0;  
 64  StringUtils.countOccurrencesOf(null, "s") == 0;  
 65  StringUtils.countOccurrencesOf("erowoiueoiur", "WERWER") == 0;  
 66  StringUtils.countOccurrencesOf("erowoiueoiur", "x")=0;  
 67  StringUtils.countOccurrencesOf("erowoiueoiur", " ") == 0;  
 68  StringUtils.countOccurrencesOf("erowoiueoiur", "") == 0;  
 69  StringUtils.countOccurrencesOf("erowoiueoiur", "e") == 2;  
 70  StringUtils.countOccurrencesOf("erowoiueoiur", "oi") == 2;  
 71  StringUtils.countOccurrencesOf("erowoiueoiur", "oiu") == 2;  
 72  StringUtils.countOccurrencesOf("erowoiueoiur", "oiur") == 1;  
 73  StringUtils.countOccurrencesOf("erowoiueoiur", "r") == 2;  
 74   
 75  //字符串替换  
 76  String inString = "a6AazAaa77abaa";  
 77  String oldPattern = "aa";  
 78  String newPattern = "foo";  
 79  // Simple replace  
 80  String s = StringUtils.replace(inString, oldPattern, newPattern);  
 81  s.equals("a6AazAfoo77abfoo")=true;  
 82   
 83  // Non match: no change  
 84  s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);  
 85  s.equals(inString)=true  
 86  s = StringUtils.replace(inString, oldPattern, null);  
 87  s.equals(inString)=true  
 88   
 89  // Null old pattern: should ignore  
 90  s = StringUtils.replace(inString, null, newPattern);  
 91         s.equals(inString)=true  
 92   
 93  //删除字符串  
 94  String inString = "The quick brown fox jumped over the lazy dog";  
 95  String noThe = StringUtils.delete(inString, "the");  
 96  noThe.equals("The quick brown fox jumped over  lazy dog")=true;  
 97  String nohe = StringUtils.delete(inString, "he");  
 98  nohe.equals("T quick brown fox jumped over t lazy dog")=true;  
 99  String nosp = StringUtils.delete(inString, " ");  
100  nosp.equals("Thequickbrownfoxjumpedoverthelazydog")=true;  
101  String killEnd = StringUtils.delete(inString, "dog");  
102  killEnd.equals("The quick brown fox jumped over the lazy ")=true;  
103  String mismatch = StringUtils.delete(inString, "dxxcxcxog");  
104   mismatch.equals(inString)=true;  
105   
106  //删除任何字符  
107  //源代码如下  
108  //char c = inString.charAt(i);  
109  //如果不存在 c 值,则返回 -1  
110  //if (charsToDelete.indexOf(c) == -1) {  
111  //out.append(c);  
112  //}  
113   
114  String inString = "Able was I ere I saw Elba";  
115   
116  String res = StringUtils.deleteAny(inString, "I");  
117         res.equals("Able was  ere  saw Elba")=true;  
118  res = StringUtils.deleteAny(inString, "AeEba!");  
119  res.equals("l ws I r I sw l")=true;  
120  String mismatch = StringUtils.deleteAny(inString, "#@$#$^");  
121  mismatch.equals(inString)=true;  
122   
123  //源代码如下 return (str != null ? "'" + str + "'" : null);  
124  assertEquals("'myString'", StringUtils.quote("myString"));  
125  assertEquals("''", StringUtils.quote(""));  
126  assertNull(StringUtils.quote(null));  
127  //将第一个字符改大写  
128  StringUtils.capitalize(Str)  
129  //将第一个个字符改小写  
130  StringUtils.uncapitalize(str)  
131   
132  //mypath/myfile.txt" -> "myfile.txt  
133  //获取字符串文件名和扩展名  
134  StringUtils.getFilename("myfile").equals("myfile")=true;  
135  StringUtils.getFilename("mypath/myfile".equals("myfile")=true;  
136  StringUtils.getFilename("mypath/myfile".equals("myfile")=true;  
137  StringUtils.getFilename("myfile.txt").equals("myfile.txt")=true;  
138  StringUtils.getFilename("mypath/myfile.txt").equals("myfile.txt")=true;  
139   
140  // 获取字符串扩展名,以.分隔  
141  StringUtils.getFilenameExtension("myfile")=null;  
142  StringUtils.getFilenameExtension("myPath/myfile")=null;  
143  StringUtils.getFilenameExtension("myfile.").equals("")=true;  
144  StringUtils.getFilenameExtension("myPath/myfile.").equals("")=true;  
145  StringUtils.StringUtils.getFilenameExtension("myfile.txt").equals("txt")=true;  
146  StringUtils.getFilenameExtension("mypath/myfile.txt").equals("txt")=true;  
147   
148  //舍去文件名扩展名  
149  StringUtils.stripFilenameExtension(null)=true;  
150  StringUtils.stripFilenameExtension("").equals("")=true;  
151  StringUtils.stripFilenameExtension("myfile").equals("myfile")=true;  
152  StringUtils.stripFilenameExtension("mypath/myfile").equals("mypath/myfile")=true;  
153  StringUtils.stripFilenameExtension("myfile.").equals("myfile")=true;  
154  StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;  
155  StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;  
156  StringUtils.stripFilenameExtension("myfile.txt").equals("myfile")=true;  
157  StringUtils.stripFilenameExtension("mypath/myfile.txt").equals("mypath/myfile")=true 

免责声明:文章转载自《Spring的StringUtils工具类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇DataTable合并列“ORA-06550: 第 1 行, 第 7 列”解决方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python数据结构与算法——桶排序

桶排序的时间复杂度是O(M+N),通过建立对原始数据的有序统计表,实现非常快速的排序过程 可以用hashtable(或者dict)实现,查询复杂度为O(1) 贴代码: 1 # 简单桶排序 从小到大 2 def bucksort(A): 3 4 bucks = dict() # 桶 5 for i in A:...

HTML标记之a标签

一、a标签的语法   <a href=”链接目标地址” title=”注释” target=”打开链接窗口的形式”>链接显示内容</a>     target值:       _blank在新窗口中打开;       _self 在自身窗口打开(默认);       _parent 在上一级窗口打开,框架会经常使用;       _...

Netty源码—七、内存释放

Netty本身在内存分配上支持堆内存和直接内存,我们一般选用直接内存,这也是默认的配置。所以要理解Netty内存的释放我们得先看下直接内存的释放。 Java直接内存释放 我们先来看下直接内存是怎么使用的 ByteBuffer.allocateDirect(capacity) 申请的过程是其实就是创建一个DirectByteBuffer对象的过程,Dire...

【微信小程序】在swiper-item使用wx:for时出现的问题

代码如下: wxml: <!--pages/mall/mall.wxml--> <view class="contianer"> <view class="swiper"> <swiper display-multiple-items="{{swiper_pictures.length}}" indic...

雷林鹏分享:C# 可空类型(Nullable)

  C# 可空类型(Nullable)   C# 可空类型(Nullable)   C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。   例如,Nullable< Int32 >,读作"可空的 Int32",可以被赋值为 -2,147,483,648 到...

ZYNQ Linux 移植:包含petalinux移植和手动移植debian9

参考: https://electronut.in/workflow-for-using-linux-on-xilinx-zynq/ https://blog.csdn.net/m0_37545528/article/details/90177983?ops_request_misc=%257B%2522request%255Fid%2522%253A%2...