1. 程式人生 > >Java之StringUtils的常用方法2

Java之StringUtils的常用方法2

1、空字串檢查

 public static boolean isEmpty(String str);
 public static boolean isNotEmpty(String str);

  上面兩個方法是判斷字串是否為”“或者null。

 public static boolean isBlank(String str);
 public static boolean isNotBlank(String str);

這兩個方法可以判斷空字串如” “同時也可以判斷是否為”“或者null

2、清除空白字元

StringUtils.trimToNull(str); 
函式介紹:清除掉str首尾的空白字元,如果僅str全由空白字元組成則返回null。

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

3、查詢巢狀字串

StringUtils.substringBetween(str,header,tail);

在str中取得header和tail之間的字串。不存在則返回空.

4、取得字串的縮寫

StringUtils.abbreviate(str,width); 
StringUtils.abbreviate(str,offset,width); 
在給定的width內取得str的縮寫,當testString的長度小於width則返回原字串. 
注意:width引數至少為4(包括…)

 String test = "This is a test of the abbreviation.";
 String test2 = "Test";
 System.out.println( StringUtils.abbreviate( test, 15 ) );
 System.out.println( StringUtils.abbreviate( test, 5,15 ) );
 System.out.println( StringUtils.abbreviate( test2, 10 ) );

輸出如下: 
This is a te… 
…is a test… 
Test

5、去除尾部換行符

 
StringUtils.chomp(str) 
去除str尾部的換行符\n

6、重複字串 
StringUtils.repeat(str,count) 
得到將str重複count次後的字串

 System.out.println( StringUtils.repeat( "*", 10));
 System.out.println( StringUtils.repeat( "China ", 5));

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

StringUtils.center( str, count,repeatString ); 
把str插入將repeatString重複多次後的字串中間,得到字串 
的總長為count

 System.out.println( StringUtils.center( "China", 11,"*"));

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

static String center(String str, int size); 
預設以空格填充 
public static String leftPad(String str,int size); 
左側空格填充 
public static String leftPad(String str,int size,String padStr); 
左側字串填充 
public static String rightPad(String str,int size); 
左側空格填充 
public static String rightPad(String str,int size,String padStr) 
左側字串填充

7、顛倒字串 
StringUtils.reverse(str) 
得到str中字元顛倒後的字串

 System.out.println( StringUtils.reverse("ABCDE"));

輸出如下: 
EDCBA

8、判斷字串內容的型別

StringUtils.isNumeric( str); 
如果str全由數字組成返回True.

StringUtils.isAlpha( str); 
如果str全由字母組成返回True.

StringUtils.isAlphanumeric( str); 
如果str全由數字或數字組成返回True.

StringUtils.isAlphaspace( str); 
如果str全由字母或空格組成返回True.

StringUtils.isAlphanumericSpace(String str); 
只由字母數字和空格組成

StringUtils.isNumericSpace(String str); 
只由數字和空格組成

9、取得某字串在另一字串中出現的次數

StringUtils.countMatches(str,seqString); 
取得seqString在str中出現的次數,未發現則返回零

 System.out.println(StringUtils.countMatches( "Chinese People", "e"));

輸出: 
4

10、部分擷取字串

StringUtils.substringBetween(testString,fromString,toString ): 
取得兩字元之間的字串

StringUtils.substringAfter(str,seqStr ): 
取得指定字串後的字串

StringUtils.substringBefore(str,seqStr ): 
取得指定字串之前的字串

StringUtils.substringBeforeLast( str,seqStr ): 
取得最後一個指定字串之前的字串

StringUtils.substringAfterLast(str,seqStr ): 
取得最後一個指定字串之後的字串

  String formatted = " 25 * (30,40) [50,60] | 30";
  System.out.print("N0: " + StringUtils.substringBeforeLast( formatted, "*" ) );
  System.out.print(", N1: " + StringUtils.substringBetween( formatted, "(", "," ) );
  System.out.print(", N2: " + StringUtils.substringBetween( formatted, ",", ")" ) );
  System.out.print(", N3: " + StringUtils.substringBetween( formatted, "[", "," ) );
  System.out.print(", N4: " + StringUtils.substringBetween( formatted, ",", "]" ) );
  System.out.print(", N5: " + StringUtils.substringAfterLast( formatted, "|" ) );

輸出: 
N0: 25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5: 30

11、首字母大寫

StringUtils.capitalize(String str); 首字母大寫 
StringUtils.uncapitalize(String str);首字母小寫

12、 是否全是大寫,是否全是小寫

StringUtils.isAllUpperCase(String str); 
StringUtils.isAllLowerCase(String str);

13、大小寫轉換,空格不動

StringUtils.swapCase(String str);

StringUtils.swapCase(“I am a-A*a”) 
返回結果:i AM A-a*A

14、陣列轉成字串

StringUtil.convString(String str); 
預設以逗號分隔 
StringUtil.converString(String str, String conv) ; 
用conv分隔

 String[3] s={“a”,”b”,”c”} 
 StringUtil.convString(s)=”a,b,c”

 String[3] s={“a”,”b”,”c”} 
 StringUtil.convString(s,”@”)=”[email protected]@c”