StringUtils的常用方法

1.取得字符串的缩写 使用函数: 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... Tes 2.查找嵌套字符串 使用函数: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 3.重复字符串 使用函数:StringUtils.repeat(repeatString,count) 函数介绍:得到将repeatString重复count次后的字符串 例程: System.out.println( StringUtils.repeat( "*", 10)); System.out.println( StringUtils.repeat( "China ", 5)); 输出如下: ********** China China China China China 其他函数:StringUtils.center( testString, count,repeatString ); 函数介绍:把testString插入将repeatString重复多次后的字符串中间,得到字符串 的总长为count 例程: System.out.println( StringUtils.center( "China", 11,"*")); 输出如下: ***China*** 4.颠倒字符串 使用函数:StringUtils.reverse(testString) 函数介绍:得到testString中字符颠倒后的字符串 例程: System.out.println( StringUtils.reverse("ABCDE")); 输出如下: EDCBA 5.空字符串检查 使用函数: StringUtils.isBlank(testString) 函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False 例程: 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 函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反. 函数StringUtils.isEmpty(str). 判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0 (当str有空格时,结果为false) 6.判断字符串内容的类型 函数介绍: StringUtils.isNumeric( testString ) :如果testString全由数字组成返回True StringUtils.isAlpha( testString ) :如果testString全由字母组成返回True StringUtils.isAlphanumeric( testString ) :如果testString全由字母或数字组 成返回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 7.取得某字符串在另一字符串中出现的次数 使用函数:StringUtils.countMatches(testString,seqString) 函数介绍:取得seqString在testString中出现的次数,未发现则返回零 例程: System.out.println(StringUtils.countMatches( "Chinese People", "e")); 输出: 4 8.字符串两端不能以指定的字符串中的字符开始或结尾, StringUtils.strip(String str, String stripChars) 函数介绍: 去掉str两端的在stripChars中的字符。 如果str为null或等于"",则返回它本身; 如果stripChars为null或"",则返回strip(String str)。 例程: System.out.println(StringUtils.strip("Hello Word", "rdH")); 输出: ello Wo 函数 StringUtils.stripStart(String str, String stripChars) 和 StringUtils.strip(String str, String stripChars) 相似,去掉str前端的在stripChars中的字符。 函数 StringUtils.stripEnd(String str, String stripChars) 和 StringUtils.strip(String str, String stripChars) 相似,去掉str末端的在stripChars中的字符。 9.删除或者替换字符串中的某个字符 使用函数:StringUtils.replace(String text, String searchString, String replacement); 函数介绍:将 text中出现的searchString替换成replacement 例程: System.out.println(StringUtils.replace("Hello Word e", "or", "")); System.out.println(StringUtils.replace("Hello Word e", " ", "")); 输出: Hello Wd e HelloWorde 10.删除或者替换字符串中的某个字符 使用函数:StringUtils.containsOnly((String str, char[] valid); 函数介绍:判断是否字符串str仅包含字符数组valid中的字符,即字符串中的字符是否都在字符数组中。如果str为null,则返回false;如果str为"",则返回true 例程: StringUtils.containsOnly(null, *)) = false StringUtils.containsOnly("", *)) = true StringUtils.containsOnly("afaf", ['a','f',' '])) = true StringUtils.containsOnly("af a", ['a','f',' '])) = true StringUtils.containsOnly("a", ['a','f',' '])) = true StringUtils.containsOnly("afg", ['a','f',' '])) = false

猜你喜欢

转载自wangtf6.iteye.com/blog/2244324