原文地址:http://abeetle.iteye.com/blog/233896
今天刚发现 StringUtils,觉得不错,特意深入了解了一下,做个说明,以供今后参考:
类包是:org.apache.commons.lang.StringUtils
Java1.4的文字处理上支持regular expression了。这可是个重量级而方便的东东啊,缺点是太复杂,学习起来有难度。相较而言,Jakarta Commons提供的StringUtils和WordUtils至今还维持着那种简洁而强大的美,使用起来也很顺手。Commons Lang的StringUtils包加入到类路径中。StringUtils类中有不计其数的有用的方法.
来个简单点的例子:
package sean.study.jakarta.commons.lang; import org.apache.commons.lang.StringUtils; public class StringUtilsAndWordUtilsUsage { public static void main(String[] args) { // data setup String str1 = ""; String str2 = " "; String str3 = "\t"; String str4 = null; String str5 = "123"; String str6 = "ABCDEFG"; String str7 = "It feels good to use Jakarta Commons.\r\n"; // check for empty strings 检查字符串是否为空或null或仅仅包含空格 System.out.println("=============================="); System.out.println("Is str1 blank? " + StringUtils.isBlank(str1)); System.out.println("Is str2 blank? " + StringUtils.isBlank(str2)); System.out.println("Is str3 blank? " + StringUtils.isBlank(str3)); System.out.println("Is str4 blank? " + StringUtils.isBlank(str4)); // check for numerics 检查字符串是否仅仅包含数字 System.out.println("=============================="); System.out.println("Is str5 numeric? " + StringUtils.isNumeric(str5)); System.out.println("Is str6 numeric? " + StringUtils.isNumeric(str6)); // reverse strings / whole words System.out.println("=============================="); System.out.println("str6: " + str6); System.out.println("str6 reversed: " + StringUtils.reverse(str6)); \\字符的全部反转 System.out.println("str7: " + str7); String str8 = StringUtils.chomp(str7); str8 = StringUtils.reverseDelimited(str8, ' '); System.out.println("str7 reversed whole words : \r\n" + str8); // build header (useful to print log messages that are easy to locate) System.out.println("=============================="); System.out.println("print header:"); String padding = StringUtils.repeat("=", 50); \\ 重复 String msg = StringUtils.center(" Customised Header ", 50, "%"); //使一个字符串居中(在输出的时候) Object[] raw = new Object[]{padding, msg, padding}; String header = StringUtils.join(raw, "\r\n"); System.out.println(header); } }
输出的结果如下:
==============================
Is str1 blank? true
Is str2 blank? true
Is str3 blank? true
Is str4 blank? true
==============================
Is str5 numeric? true
Is str6 numeric? false
==============================
str6: ABCDEFG
str6 reversed: GFEDCBA
str7: It feels good to use Jakarta Commons.
str7 reversed whole words :
Commons. Jakarta use to good feels It
==============================
print header:
==================================================
%%%%%%%%%%%%%%% Customised Header %%%%%%%%%%%%%%%%
==================================================
除此之外,还有比较常用的用法:
如果你需要重复的书写同一个数字,你可以这么写:
log(StringUtils.leftPad("34", 8, "0"));
// 00000034
又或者你需要在一个数组中加入一个元素,你可以这么做:
log(StringUtils.join(new String[]{"cat","dog","carrot","leaf","door"}, ":")
// cat:dog:carrot:leaf:door
如果你需要一个大写字母或者是需要一个字符串中的每个单词第一个字母大写,你可以这么做:
log(StringUtils.capitaliseAllWords("a sentenced to be capitalised"));
// A Sentenced To Be Capitalised
如果你需要计算一个字母在字符串中出现的个数,你可以使用countMatches方法:log(StringUtils.countMatches("Bethany plays with army men", "e"));
// 2
甚至还有计算两字符串之间的Levenshtein-Distance
log(StringUtils.getLevenshteinDistance("David", "Jakob"));
// 4
分解字符串
StringUtils.split(null, *, *) = null
StringUtils.split("", *, *) = []
StringUtils.split("ab de fg", null, 0) = ["ab", "cd", "ef"]
StringUtils.split("ab de fg", null, 0) = ["ab", "cd", "ef"]
StringUtils.split("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"]
StringUtils.split("ab:cd:ef", ":", 1) = ["ab:cd:ef"]
StringUtils.split("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
StringUtils.split(String str,String separatorChars,int max) str为null时返回null
separatorChars为null时默认为按空格分解,max为0或负数时分解没有限制,max为1时返回整个字符串,max为分解成的个数(大于实际则无效)
去除字符串前后指定的字符
StringUtils.strip(null, *) = null
StringUtils.strip("", *) = ""
StringUtils.strip("abc", null) = "abc"
StringUtils.strip(" abc ", null) = "abc"
StringUtils.strip(" abcyx", "xyz") = " abc"
StringUtils.strip(String str,String stripChars) str为null时返回null,stripChars为null时默认为空格
创建醒目的Header(调试时用)
public String createHeader( String title ) {
int width = 30;
String stars = StringUtils.repeat( "*", width);
String centered = StringUtils.center( title, width, "*" );
String heading = StringUtils.join(new Object[]{stars, centered, stars}, "\n");
return heading;
}
调用createHeader("TEST")的输出结果:
******************************
************ TEST ************
******************************
检查字符串是否仅仅包含数字、字母或数字和字母的混合
String test1 = "ORANGE";
String test2 = "ICE9";
String test3 = "ICE CREAM";
String test4 = "820B Judson Avenue";
String test5 = "1976";
结果:
boolean t1val = StringUtils.isAlpha( test1 ); // returns true
boolean t2val = StringUtils.isAlphanumeric( test2 ); // returns true
boolean t3val = StringUtils.isAlphaSpace( test3 ); // returns true
boolean t4val = StringUtils.isAlphanumericSpace( test4 ); // returns true
boolean t5val = StringUtils.isNumeric( test5 ); // returns true