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
作者: Acode
出处: http://www.cnblogs.com/acode/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可留言咨询.