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  ainsWhitespace(null)=false;
14  ainsWhitespace("")=false;
15  ainsWhitespace("a")=false;
16  ainsWhitespace("abc")=false;
17  ainsWhitespace("abc")=false;
18  ainsWhitespace(" ")=true;
19  ainsWhitespace(" a")=true;
20  ainsWhitespace("abc ")=true;
21  ainsWhitespace("a b")=true
22  ainsWhitespace("a  b")
23
24  imWhitespace(null)=null;
25  imWhitespace("")="";
26  imWhitespace(" ")="";
27  imWhitespace("/t")="";
28  imWhitespace(" a")="a";
29  imWhitespace("a ")="a";
30  imWhitespace(" a ")="a";
31  imWhitespace(" a b ")="a b";
32
33  imLeadingWhitespace(null)=null;
34  imLeadingWhitespace("")="";
35  imLeadingWhitespace(" ")="";
36  imLeadingWhitespace("/t")="";
37  imLeadingWhitespace(" a")="a";
38  imLeadingWhitespace("a ")="a ";
39  imLeadingWhitespace(" a ")="a ";
40  imLeadingWhitespace(" a b ")="a b "
41  imLeadingWhitespace(" a b  c ")="a b  c "
42
43  imTrailingWhitespace(null)=null;
44  imTrailingWhitespace(" ")="";
45  imTrailingWhitespace("/t")="";
46  imTrailingWhitespace("a ")="a";
47  imTrailingWhitespace(" a")=" a";
48  imTrailingWhitespace(" a ")=" a";
49  imTrailingWhitespace(" a b ")=" a b";
50  imTrailingWhitespace(" a b  c ")=" a b  c";
51
52
53  imAllWhitespace("")="";
54  imAllWhitespace(" ")="";
55  imAllWhitespace("/t")="";
56  imAllWhitespace(" a")="a";
57  imAllWhitespace("a ")="a";
58  imAllWhitespace(" a ")="a";
59  imAllWhitespace(" a b ")="ab";
60  imAllWhitespace(" a b  c "="abc";
61// 统计⼀个⼦字符串在字符串出现的次数
62  untOccurrencesOf(null, null) == 0;
63  untOccurrencesOf("s", null) == 0;
64  untOccurrencesOf(null, "s") == 0;
65  untOccurrencesOf("erowoiueoiur", "WERWER") == 0;
66  untOccurrencesOf("erowoiueoiur", "x")=0;
67  untOccurrencesOf("erowoiueoiur", " ") == 0;
68  untOccurrencesOf("erowoiueoiur", "") == 0;
69  untOccurrencesOf("erowoiueoiur", "e") == 2;
70  untOccurrencesOf("erowoiueoiur", "oi") == 2;
71  untOccurrencesOf("erowoiueoiur", "oiu") == 2;
72  untOccurrencesOf("erowoiueoiur", "oiur") == 1;
73  untOccurrencesOf("erowoiueoiur", "r") == 2;
74
75//字符串替换
76  String inString = "a6AazAaa77abaa";
77  String oldPattern = "aa";
78  String newPattern = "foo";
79// Simple replace
80  String s = place(inString, oldPattern, newPattern);
81  s.equals("a6AazAfoo77abfoo")=true;
82
83// Non match: no change
84  s = place(inString, "qwoeiruqopwieurpoqwieur", newPattern);
85  s.equals(inString)=true
86  s = place(inString, oldPattern, null);
87  s.equals(inString)=true
88
89// Null old pattern: should ignore
90  s = place(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
wispring是什么意思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//" -> "
133//获取字符串⽂件名和扩展名
134  Filename("myfile").equals("myfile")=true;
135  Filename("mypath/myfile".equals("myfile")=true;
136  Filename("mypath/myfile".equals("myfile")=true;
137  Filename("").equals("")=true;
138  Filename("").equals("")=true;
139
140// 获取字符串扩展名,以.分隔
141  FilenameExtension("myfile")=null;
142  FilenameExtension("myPath/myfile")=null;
143  FilenameExtension("myfile.").equals("")=true;
144  FilenameExtension("myPath/myfile.").equals("")=true;
145  FilenameExtension("").equals("txt")=true;
146  FilenameExtension("").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("").equals("myfile")=true;
157  StringUtils.stripFilenameExtension("").equals("mypath/myfile")=true

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。