在org.apache.commons包中提供了的一系列能簡(jiǎn)化一些編程過程中常見問題的共通函數(shù)和類,使程序員能把主要精力集中在
構(gòu)架,業(yè)務(wù)實(shí)現(xiàn)和優(yōu)化而不是具體實(shí)現(xiàn)及驗(yàn)證上,一言以蔽之,它能使我們避免重復(fù)的發(fā)明車輪。
org.apache.commons包的下載頁面在:
http://jakarta.apache.org/site/downloads/downloads_commons-lang.cgi其中源碼大家可以借鑒一下,我覺得很有參考價(jià)值,尤其是有些函數(shù)在不用正則表達(dá)式下取得的效果。
取得commons-lang-2.1.jar后加入自己工程的lib目錄就可以了.如果用戶不允許使用commons,那末打開其源碼把具體函數(shù)加入自己的代碼也可以,當(dāng)然需要尊重人家的知識(shí)產(chǎn)權(quán)。
以下代碼經(jīng)過測(cè)試,測(cè)試環(huán)境(WinXp+Eclipse3.1+JDK1.5+commons-lang-2.1),我在有些地方修改了一下。
Jakarta Commons Cookbook—01—Manipulating Text
Commons之字符串操作
要利用Jakarta Commons來進(jìn)行字符串操作,首先需要加載需要用到的包:
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
以下是StringUtils的各項(xiàng)用法
1.空字符串檢查
使用函數(shù): StringUtils.isBlank(testString)
函數(shù)介紹: 當(dāng)testString為空,長(zhǎng)度為零或者僅由空白字符(whitespace)組成時(shí),返回True;否則返回False
例程:
??? String test = "";
??? String test2 = "\n\n\t";
??? String test3 = null;
??? String test4 = "Test";
??? System.out.println( "test blank? " + StringUtils.isBlank( test ) );
??? System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );
??? System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );
??? System.out.println( "test4 blank? " + StringUtils.isBlank( test4 ) );
輸出如下:
test blank? true
test2 blank? true
test3 blank? true
test4 blank? False
函數(shù)StringUtils.isNotBlank(testString)的功能與StringUtils.isBlank(testString)相反.
2.清除空白字符
使用函數(shù): StringUtils.trimToNull(testString)
函數(shù)介紹:清除掉testString首尾的空白字符,如果僅testString全由空白字符
(whitespace)組成則返回null
例程:
??? String test1 = "\t";
??? String test2 = "? A? Test? ";
??? String test3 = null;
??? System.out.println( "test1 trimToNull: " + StringUtils.trimToNull( test1 ) );
??? System.out.println( "test2 trimToNull: " + StringUtils.trimToNull( test2 ) );
??? System.out.println( "test3 trimToNull: " + StringUtils.trimToNull( test3 ) );
輸出如下:
test1 trimToNull: null
test2 trimToNull: A? Test
test3 trimToNull: null
注意:函數(shù)StringUtils.trim(testString)與
StringUtils.trimToNull(testString)功能類似,但testString由空白字符
(whitespace)組成時(shí)返回零長(zhǎng)度字符串。
3.取得字符串的縮寫
使用函數(shù): StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)
函數(shù)介紹:在給定的width內(nèi)取得testString的縮寫,當(dāng)testString的長(zhǎng)度小于width則返回原字符串.
例程:
??? 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
4.劈分字符串
使用函數(shù): StringUtils.split(testString,splitChars,arrayLength)
函數(shù)介紹:splitChars中可以包含一系列的字符串來劈分testString,并可以設(shè)定得
到數(shù)組的長(zhǎng)度.注意設(shè)定長(zhǎng)度arrayLength和劈分字符串間有抵觸關(guān)系,建議一般情況下
不要設(shè)定長(zhǎng)度.
例程:
??? String input = "A b,c.d|e";
??? String input2 = "Pharmacy, basketball funky";
???
??? String[] array1 = StringUtils.split( input, " ,.|");
??? String[] array2 = StringUtils.split( input2, " ,", 2 );
??? System.out.println( ArrayUtils.toString( array1 ) );
??? System.out.println( ArrayUtils.toString( array2 ) );
輸出如下:
{A,b,c,d,e}
{Pharmacy,basketball funky}
5.查找嵌套字符串
使用函數(shù):StringUtils.substringBetween(testString,header,tail)
函數(shù)介紹:在testString中取得header和tail之間的字符串。不存在則返回空
例程:
??? String htmlContent = "ABC1234ABC4567";
??? System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));
??? System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567"));
輸出如下:
??? ABC
??? null
6.去除尾部換行符
使用函數(shù):StringUtils.chomp(testString)
函數(shù)介紹:去除testString尾部的換行符
例程:
??? String input = "Hello\n";
??? System.out.println( StringUtils.chomp( input ));
??? String input2 = "Another test\r\n";
??? System.out.println( StringUtils.chomp( input2 ));
輸出如下:
??? Hello
??? Another test
7.重復(fù)字符串
使用函數(shù):StringUtils.repeat(repeatString,count)
函數(shù)介紹:得到將repeatString重復(fù)count次后的字符串
例程:
??? System.out.println( StringUtils.repeat( "*", 10));
??? System.out.println( StringUtils.repeat( "China ", 5));
輸出如下:
??? **********
??? China China China China China
其他函數(shù):StringUtils.center( testString, count,repeatString );
函數(shù)介紹:把testString插入將repeatString重復(fù)多次后的字符串中間,得到字符串
的總長(zhǎng)為count
例程:
??? System.out.println( StringUtils.center( "China", 11,"*"));
輸出如下:
??? ***China***
8.顛倒字符串
使用函數(shù):StringUtils.reverse(testString)
函數(shù)介紹:得到testString中字符顛倒后的字符串
例程:
??? System.out.println( StringUtils.reverse("ABCDE"));
輸出如下:
??? EDCBA
9.判斷字符串內(nèi)容的類型
函數(shù)介紹:
StringUtils.isNumeric( testString ) :如果testString全由數(shù)字組成返回True
StringUtils.isAlpha( testString ) :如果testString全由字母組成返回True
StringUtils.isAlphanumeric( testString ) :如果testString全由數(shù)字或數(shù)字組
成返回True
StringUtils.isAlphaspace( testString )? :如果testString全由字母或空格組
成返回True
例程:
??? String state = "Virginia";
??? System.out.println( "Is state number? " + StringUtils.isNumeric(
state ) );
??? System.out.println( "Is state alpha? " + StringUtils.isAlpha( state )
);
??? System.out.println( "Is state alphanumeric? " +StringUtils.isAlphanumeric( state ) );
??? 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.取得某字符串在另一字符串中出現(xiàn)的次數(shù)
使用函數(shù):StringUtils.countMatches(testString,seqString)
函數(shù)介紹:取得seqString在testString中出現(xiàn)的次數(shù),未發(fā)現(xiàn)則返回零
例程:
??? System.out.println(StringUtils.countMatches( "Chinese People", "e"
));
輸出:
??? 4
11.部分截取字符串
使用函數(shù):
StringUtils.substringBetween(testString,fromString,toString ):取得兩字符
之間的字符串
StringUtils.substringAfter( ):取得指定字符串后的字符串
StringUtils.substringBefore( ):取得指定字符串之前的字符串
StringUtils.substringBeforeLast( ):取得最后一個(gè)指定字符串之前的字符串
StringUtils.substringAfterLast( ):取得最后一個(gè)指定字符串之后的字符串
函數(shù)介紹:上面應(yīng)該都講明白了吧。
例程:
??? 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