StringUtil 簡單用法

轉自:http://qinya.iteye.com/blog/630708

org.apache.commons.lang.StringUtil(StringUtil包函數(用法)) 

Java代碼  收藏代碼
  1. import org.apache.commons.beanutils.BeanUtils;  
  2. import org.apache.commons.beanutils.ConvertUtils;  
  3. import org.apache.commons.beanutils.LazyDynaBean;  
  4. import org.apache.commons.beanutils.converters.BigDecimalConverter;  
  5. import org.apache.commons.beanutils.converters.DoubleConverter;  
  6. import org.apache.commons.beanutils.converters.IntegerConverter;  
  7. import org.apache.commons.beanutils.converters.LongConverter;   
  8. import org.apache.commons.lang.StringUtils;  

1.空字符串檢查 
使用函數: StringUtils.isBlank(testString) 
函數介紹: 當testString爲空,長度爲零或者僅由空白字符(whitespace)組成時,返回True;否則返回False 
例程: 
   
Java代碼  收藏代碼
  1. String test = "";  
  2.     String test2 = "\n\n\t";  
  3.     String test3 = null;  
  4.     String test4 = "Test";   
  5.     System.out.println( "test blank? " + StringUtils.isBlank( test ) );  
  6.     System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );  
  7.     System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );  
  8.     System.out.println( "test4 blank? " + StringUtils.isBlank( test4 ) );  
輸出如下: 
test blank? true 
test2 blank? true 
test3 blank? true 
test4 blank? False 
函數StringUtils.isNotBlank(testString)的功能與StringUtils.isBlank(testString)相反. 


2.清除空白字符 
使用函數: StringUtils.trimToNull(testString) 
函數介紹:清除掉testString首尾的空白字符,如果僅testString全由空白字符 
(whitespace)組成則返回null 
例程: 
   
Java代碼  收藏代碼
  1. String test1 = "\t";  
  2.     String test2 = "  A  Test  ";  
  3.     String test3 = null;  
  4.   
  5.     System.out.println( "test1 trimToNull: " + StringUtils.trimToNull( test1 ) );  
  6.     System.out.println( "test2 trimToNull: " + StringUtils.trimToNull( test2 ) );  
  7.     System.out.println( "test3 trimToNull: " + StringUtils.trimToNull( test3 ) );  


輸出如下: 
test1 trimToNull: null 
test2 trimToNull: A  Test 
test3 trimToNull: null 

注意:函數StringUtils.trim(testString)與 
StringUtils.trimToNull(testString)功能類似,但testString由空白字符 
(whitespace)組成時返回零長度字符串。 


3.取得字符串的縮寫 
使用函數: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width) 
函數介紹:在給定的width內取得testString的縮寫,當testString的長度小於width則返回原字符串. 
例程: 
   
Java代碼  收藏代碼
  1. String test = "This is a test of the abbreviation.";  
  2.     String test2 = "Test";  
  3.   
  4.     System.out.println( StringUtils.abbreviate( test, 15 ) );  
  5.     System.out.println( StringUtils.abbreviate( test, 5,15 ) );  
  6.     System.out.println( StringUtils.abbreviate( test2, 10 ) );  
輸出如下: 
This is a te... 
...is a test... 
Test 

4.劈分字符串 
使用函數: StringUtils.split(testString,splitChars,arrayLength) 
函數介紹:splitChars中可以包含一系列的字符串來劈分testString,並可以設定得 
到數組的長度.注意設定長度arrayLength和劈分字符串間有牴觸關係,建議一般情況下 
不要設定長度. 
例程: 
   
Java代碼  收藏代碼
  1. String input = "A b,c.d|e";  
  2.     String input2 = "Pharmacy, basketball funky";  
  3.       
  4.   
  5.     String[] array1 = StringUtils.split( input, " ,.|");  
  6.     String[] array2 = StringUtils.split( input2, " ,"2 );  
  7.   
  8.   
  9.     System.out.println( ArrayUtils.toString( array1 ) );  
  10.     System.out.println( ArrayUtils.toString( array2 ) );  
輸出如下: 
{A,b,c,d,e} 
{Pharmacy,basketball funky} 

5.查找嵌套字符串 
使用函數:StringUtils.substringBetween(testString,header,tail) 
函數介紹:在testString中取得header和tail之間的字符串。不存在則返回空 
例程: 
   
Java代碼  收藏代碼
  1. String htmlContent = "ABC1234ABC4567";  
  2.     System.out.println(StringUtils.substringBetween(htmlContent, "1234""4567"));  
  3.     System.out.println(StringUtils.substringBetween(htmlContent, "12345""4567"));  
輸出如下: 
    ABC 
    null 


6.去除尾部換行符 
使用函數:StringUtils.chomp(testString) 
函數介紹:去除testString尾部的換行符 
例程: 
   
Java代碼  收藏代碼
  1. String input = "Hello\n";  
  2.     System.out.println( StringUtils.chomp( input ));  
  3.     String input2 = "Another test\r\n";  
  4.     System.out.println( StringUtils.chomp( input2 ));  
輸出如下: 
    Hello 
    Another test 


7.重複字符串 
使用函數:StringUtils.repeat(repeatString,count) 
函數介紹:得到將repeatString重複count次後的字符串 
例程: 
   
Java代碼  收藏代碼
  1. System.out.println( StringUtils.repeat( "*"10));  
  2.     System.out.println( StringUtils.repeat( "China "5));  

輸出如下: 
    ********** 
    China China China China China 

其他函數:StringUtils.center( testString, count,repeatString ); 
函數介紹:把testString插入將repeatString重複多次後的字符串中間,得到字符串 
的總長爲count 
例程: 
   
Java代碼  收藏代碼
  1. System.out.println( StringUtils.center( "China"11,"*"));  
輸出如下: 
    ***China*** 


8.顛倒字符串 
使用函數:StringUtils.reverse(testString) 
函數介紹:得到testString中字符顛倒後的字符串 
例程: 
   
Java代碼  收藏代碼
  1. System.out.println( StringUtils.reverse("ABCDE"));  
輸出如下: 
    EDCBA 

9.判斷字符串內容的類型 
函數介紹: 
StringUtils.isNumeric( testString ) :如果testString全由數字組成返回True 
StringUtils.isAlpha( testString ) :如果testString全由字母組成返回True 
StringUtils.isAlphanumeric( testString ) :如果testString全由數字或數字組 
成返回True 
StringUtils.isAlphaspace( testString )  :如果testString全由字母或空格組 
成返回True 

例程: 
   
Java代碼  收藏代碼
  1. String state = "Virginia";  
  2.     System.out.println( "Is state number? " + StringUtils.isNumeric(state ) );  
  3.     System.out.println( "Is state alpha? " + StringUtils.isAlpha( state ));  
  4.     System.out.println( "Is state alphanumeric? " +StringUtils.isAlphanumeric( state ) );  
  5.     System.out.println( "Is state alphaspace? " + StringUtils.isAlphaSpace( state ) );  
輸出如下: 
    Is state number? false 
    Is state alpha? true 
    Is state alphanumeric? true 
    Is state alphaspace? true 

10.取得某字符串在另一字符串中出現的次數 
使用函數:StringUtils.countMatches(testString,seqString) 
函數介紹:取得seqString在testString中出現的次數,未發現則返回零 
例程: 
    System.out.println(StringUtils.countMatches( "Chinese People", "e")); 
輸出: 
    4 

11.部分截取字符串 
使用函數: 
StringUtils.substringBetween(testString,fromString,toString ):取得兩字符 
之間的字符串 
StringUtils.substringAfter( ):取得指定字符串後的字符串 
StringUtils.substringBefore( ):取得指定字符串之前的字符串 
StringUtils.substringBeforeLast( ):取得最後一個指定字符串之前的字符串 
StringUtils.substringAfterLast( ):取得最後一個指定字符串之後的字符串 

函數介紹:上面應該都講明白了吧。 
例程: 
   
Java代碼  收藏代碼
  1. String formatted = " 25 * (30,40) [50,60] | 30";  
  2.     System.out.print("N0: " + StringUtils.substringBeforeLast( formatted, "*" ) );  
  3.     System.out.print(", N1: " + StringUtils.substringBetween( formatted, "(""," ) );  
  4.     System.out.print(", N2: " + StringUtils.substringBetween( formatted, ","")" ) );  
  5.     System.out.print(", N3: " + StringUtils.substringBetween( formatted, "[""," ) );  
  6.     System.out.print(", N4: " + StringUtils.substringBetween( formatted, ",""]" ) );  
  7.     System.out.print(", N5: " + StringUtils.substringAfterLast( formatted, "|" ) );  
輸出如下: 
    N0:  25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5:  30 

=========================================================================================================
Java代碼  收藏代碼
  1. String sName = "Java轉義字符(補遺)";  
  2. sName = sName.replaceFirst("(補遺)","");  
  3. out.println(sName);  


如果你以爲會輸出“Java轉義字符”,那你就錯了,事實上輸出“Java轉義字符()”,我也很奇怪,以爲是中英文括號的問題,可是並不是,我不確定是否轉義問題,解決方法是 

Java代碼  收藏代碼
  1. sName = sName.replaceFirst("\\(補遺\\)","");  

發佈了6 篇原創文章 · 獲贊 18 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章