妙用Commons良药<二>
谈谈怎么用org.apache.commons.lang.StringUtils这一个类,讲一下具体的几个常用的方法
使用StringUtils.isBlank(),该方法在收到空字符串,零长度字符串或全为空格的字符串时将返回true.它能通过返回true的方式优雅地处理null.
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 ) );
前面三个返回true,返回一个返回false
同样,有一个StringUtils.isNotBlank()方法,当一个字符串里面为空,或只包含空格,或是null时,该方法将返回false,该方法的作用相当于:
if( variable != null && variable.length( ) > 0 && !variable.trim( ).equals("") ) {
// Do something
}
接下来,介绍一个非常有用的方法StringUtils.abbreviate(),该方法接受一个字符串,如果可以的话就缩减它,如果要把一个字符串缩减为20个字符长,而原字符串长度小于20个字符长,则该方法将返回原文本.若原字符串长度大于20个字符,则显示17个字符和3个省略号.
String test = "This is a test of the abbreviation."
String test2 = "Test"
System.out.println( StringUtils.abbreviate( test, 10 ) );
System.out.println( StringUtils.abbreviate( test2, 10 ) );
结果如下:
This is...
Test
另外一点,可以使用StringUtils.split(),并提供一系列字符作为分隔符,StringUtils.chomp()去除字符串的末行终止序列.
接下来,看一下StringUtils.substringBetween()方法,看下面一个例子:
String variables = "{45}, {35}, {120}" ;
List numbers = new ArrayList( );
String variablesTemp = variables;
while( StringUtils.substringBetween( variablesTemp, "{", "}" ) != null ) {
String numberStr = StringUtils.substringBetween( variables, "{", "}" );
Double number = new Double( numberStr );
numbers.add( number );
variablesTemp = variablesTemp.substring( variablesTemp.indexOf(",") );
}
double sum = StatUtil.sum( ArrayUtils.toPrimitive( numbers.toArray( ) ) );
System.out.println( "Variables: " + variables + ", Sum: " + sum );
其结果如下:
Variable: {45}, {35}, {120}, Sum: 200
如果字符串的首尾含有需要清除的字符,可使用StringUtils.strip()清除它们.看下面例子:
String original = "-------***---SHAZAM!---***-------";
String stripped = StringUtils.strip( original, "-*" );
System.out.println( "Stripped: " + stripped )
结果如下:
Stripped: SHAZAM!
增加一点,StringUtils.reverse()可以实现字符串的反转,StringUtils.reverseDelimited()能够分隔符反向排列字符串中的标记.举例:
public Sentence reverseSentence(String sentence) {
String reversed = StringUtils.chomp( sentence, "." );
reversed = StringUtils.reverseDelimited( reversed, ' ' );
reversed = reversed + ".";
return reversed;
}
String sentence = "I am Susan."
String reversed = reverseSentence( sentence ) );
System.out.println( sentence );
System.out.println( reversed );
结果如下:
I am Susan.
Susan am I.:
当然,还可以检测字符串内容,isNumeric( ),isAlpha(),isAlphanumeric(),and isAlphaSpace()这几个方法用来验证用户输入的正确性,验证字符串的内容是否为数字,是否字符,是否为数字+字符,是否为字符+空格.
如果你需要检查一个大文件里面一个字符串出现的次数,可以为StringUtils.countMatches()这一个方法,下面举一个比较好的例子,不能将整个文件放入内存中,相对比较合理的做法是一次一行地统计,其方法如下:
public int testNumber() throws Exception{
File manuscriptFile = new File("c:\\test.txt");
Reader reader = new FileReader(manuscriptFile);
LineNumberReader lineReader = new LineNumberReader(reader);
int number = 0;
while(lineReader.ready()){
String line = StringUtils.lowerCase(lineReader.readLine());
number += StringUtils.countMatches(line, "test");
}
return number;
}
分享到:
相关推荐
<artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
添加依赖: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core... <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
<artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </...
<version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.spring...
<artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </...
struts-1.3.8 包(<br><br>antlr-2.7.2.jar<br>bsf-2.3.0.jar<br>commons-beanutils-1.7.0.jar<br>commons-chain-1.1.jar<br>commons-digester-1.8.jar<br>commons-fileupload-1.1.1.jar<br>commons-io-1.1.jar<br>...
<artifactId>standard</artifactId> <version>1.1.2</version> <artifactId>jstl</artifactId> ... <artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>javax.servlet-api</artifactId>
<artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>mybatis</artifactId> <version>3.5.5</version> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> ...
其中包含:shale-core.jar<br>commons-beanutils.jar<br>commons-chain.jar<br>commons-codec.jar<br>commons-collections.jar<br>commons-digester.jar<br>commons-el.jar<br>commons-fileupload.jar<br>commons-...
<module>cloud-api-commons</module> <module>cloud-eureka-server7001</module> <module>cloud-eureka-server7002</module> <module>cloud-comsumer-order80</module> <module>cloud-provider-payment8001</...
collections-2.1.1.jar<br>commons-logging-1.0.4.jar<br>dom4j-1.6.1.jar<br>ehcache-1.1.jar<br>hibernate3.jar<br>jaas.jar<br>jaxen-1.1-beta-7.jar<br>jdbc2_0-stdext.jar<br>jta.jar<br>log4j-1.2.11.jar<br>...
api.jar<br>jxl.jar<br>itext-1.3.jar<br>poi-2.5.1.jar<br>cos.jar<br>velocity-1.5.jar<br>velocity-tools-view-1.3.jar<br>commons-codec.jar<br>antlr.jar<br>commons-beanutils.jar<br>commons-digester.jar<br...
ssm在maven项目管理下的pom文件,jar包依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> ... <artifactId>commons-codec</artifactId> <version>1.10</ver