日期:2014-05-17  浏览次数:20850 次

org.apache.commons包介绍及应用
org.apache.commons是apache组下的一个项目。在org.apache.commons包中提供了的一系列能简化一些编程过程中常见问题的共通函数和类,分别成为独立的jar包,当前共有36个jar包。该项目的目的是创建一系列可重用的JAVA组件,使程序员能把主要精力集中在构架,业务实现和优化而不是具体实现及验证上。一言以蔽之,它能使我们避免重复的发明车轮。
org.apache.commons包的下载页面在:
http://commons.apache.org/
其中源码大家可以借鉴一下,我觉得很有参考价值,尤其是有些函数在不用正则表达式下取得的效果。

取得commons-lang-2.1.jar后加入自己工程的lib目录就可以了.如果用户不允许使用commons,那末打开其源码把具体函数加入自己的代码也可以,当然需要尊重人家的知识产权。
以下代码经过测试,测试环境(WinXp+Eclipse3.1+JDK1.5+commons-lang-2.1),我在有些地方修改了一下。

Jakarta Commons Cookbook—01—Manipulating Text
Commons之字符串操作
要利用Jakarta Commons来进行字符串操作,首先需要加载需要用到的包:
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;

以下是StringUtils的各项用法
1.空字符串检查
使用函数: StringUtils.isBlank(testString)
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回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

函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反.


2.清除空白字符
使用函数: StringUtils.trimToNull(testString)
函数介绍:清除掉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

注意:函数StringUtils.trim(testString)与
StringUtils.trimToNull(testString)功能类似,但testString由空白字符
(whitespace)组成时返回零长度字符串。


3.取得字符串的缩写
使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)
函数介绍:在给定的width内取得testString的缩写,当testString的长度小于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.劈分字符串
使用函数: StringUtils.split(testString,splitChars,arrayLength)
函数介绍:splitChars中可以包含一系列的字符串来劈分testString,并可以设定得
到数组的长度.注意设定长度arrayLength和劈分字符串间有抵触关系,建议一般情况下
不要设定长度.
例程:
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.查找嵌套字符串
使用函数:StringUtils.substringBetween(testString,header,tail)
函数介绍:在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.去除尾部换行符
使用函数:StringUtils.chomp(testString)
函数介绍:去除testString尾部的换行符
例程:
String inpu