org.springframework.util.StringUtils的使用

來源:互聯網
上載者:User

我們經常會對字串進行操作,spring已經實現了常用的處理功能。我們可以使用org.springframework.util.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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.