Spring 工具类 StringUtils

我们经常会对字符串进行操作,在 Spring 中可以使用 org.springframework.util.StringUtils 工具类帮我们处理字符串。

常用方法如下:

判断长度,包括空格

1
2
3
4
StringUtils.hasLength(null) = false;
StringUtils.hasLength("") = false;
StringUtils.hasLength(" ") = true;
StringUtils.hasLength("Hello") = true;

判断是否包含文本

1
2
3
4
5
StringUtils.hasText(null) = false;
StringUtils.hasText("") = false;
StringUtils.hasText(" ") = false;
StringUtils.hasText("12345") = true;
StringUtils.hasText(" 12345 ") = true;

是否包含空白字符

1
2
3
4
5
6
7
8
9
10
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") = true;

去空格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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";

统计一个子字符串在字符串出现的次数

1
2
3
4
5
6
7
8
9
10
11
12
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;

获取给定完整路径的简单文件名

1
2
3
4
5
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;

获取文件名字符串的扩展名

1
2
3
4
5
6
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;

去掉文件名的扩展名

1
2
3
4
5
6
7
8
9
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;