开发中需要写很多工具类,大多数是关于字符串、文件、JavaBean、集合等。写这些类之前建议你看看apache下的common包,如果有就不必再造轮子了,但是common的源代码还是值得我们研究的。本篇的重点是讲解common包下StringUtils和WordUtils。你需要在测试类中导入
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
1.StringUtils.isBlank 检查字符串是否是空字符串或null
StringUtils.isBlank方法将返回true如果输入字符是空字符串、或者长度为0、或者是空白符、或者是null。
String test = "";
String test2 = "\n\n\t";
String test3 = null;
String test4 = "Test";
String test5 = " ";
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 ) );
System.out.println( "test5 blank? " + StringUtils.isBlank( test5 ) );
输出结果如下:
test blank? true
test2 blank? true
test3 blank? true
test4 blank? false
test5 blank? true
2.StringUtils.isNotBlank检查字符串是否不是空字符串或不是null
StringUtils.isNotBlank与StringUtils.isBlank相反,如果输入字符是空字符串、或者长度为0、或者是空白符、或者是null返回false。
String test = "\t\t";
String test2 = "Test";
System.out.println( "test is not blank? " + StringUtils.isNotBlank( test ) );
System.out.println( "test2 is not blank? " + StringUtils.isNotBlank( test2 ) );
输出结果如下:
test is not blank? false
test2 is not blank? true
3.StringUtils.trimToNull 去掉字符串前面和后面的空字符
StringUtils.trimToNull去掉一个字符串前面和后面的空格符或者空字符串,如果去掉结果是空字符则返回null,如果一个字符串为null则返回null。
String test1 = "\t";
String test2 = "Test";
String test3 = null;
String test4 ="";
String test5 =" ";
String test6 =" ddd * ";
System.out.println( "test1 trimToNull: " + StringUtils.trimToNull( test1 ) );
System.out.println( "test2 trimToNull: " + StringUtils.trimToNull( test2 ) );
System.out.println( "test3 trimToNull: " + StringUtils.trimToNull( test3 ) );
System.out.println( "test4 trimToNull: " + StringUtils.trimToNull( test4 ) );
System.out.println( "test5 trimToNull: " + StringUtils.trimToNull( test5 ) );
System.out.println( "test6 trimToNull: " + StringUtils.trimToNull( test6 ) );
输出结果如下:
test1 trimToNull: null
test2 trimToNull: Test
test3 trimToNull: null
test4 trimToNull: null
test5 trimToNull: null
test6 trimToNull: ddd *
以上三个方法虽然很简单,但是在开发中用的几率很高。如果不用以上该方法,我们经常要写以下代码
if( variable != null &&
variable.length( ) > 0 &&
!variable.trim( ).equals("") ) {
// Do something
}
以上很繁琐的代码,有了common包,就可以这样写了
if( StringUtils.isNotBlank( variable ) ) {
// Do something
}
4. StringUtils.abbreviate 缩减字符串
电脑的任务栏中,如果任务栏的标题过长,会显示成"***********...".这种效果就是字符串的缩减。
StringUtils.abbreviate(str, maxWidth);第一个参数是输入的字符串 第二个参数是最大的宽度。
StringUtils.abbreviate(str, offset, maxWidth);第一个参数是输入的字符串,第二个参数是开始缩减的位置,第三个参数是最大的宽度。
String test = "123456789";
String test2 = "Test";
System.out.println(StringUtils.abbreviate(test, 5));
System.out.println(StringUtils.abbreviate(test2, 10));
String message = "There was a palpable sense that the rest of the world ";
int index = message.indexOf("sense");
int offset = index - 5;
int width = 15;
String context = StringUtils.abbreviate(message, offset, width);
System.out.println("The word 'ground' in context: " + context);