StringUtils用法

 

前言:
在org.apache.commons包中提供了的一系列能简化一些编程过程中常见问题的共通函数和类,使程序员能把主要精力集中在
构架,业务实现和优化而不是具体实现及验证上,一言以蔽之,它能使我们避免重复的发明车轮。

org.apache.commons包的下载页面在:
http://jakarta.apache.org/site/downloads/downloads_commons-lang.cgi
其中源码大家可以借鉴一下,我觉得很有参考价值,尤其是有些函数在不用正则表达式下取得的效果。

取得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 input = "Hello/n";
    System.out.println( StringUtils.chomp( input ));
    String input2 = "Another test/r/n";
    System.out.println( StringUtils.chomp( input2 ));
输出如下:
    Hello
    Another test


7.重复字符串
使用函数: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***


8.颠倒字符串
使用函数:StringUtils.reverse(testString)
函数介绍:得到testString中字符颠倒后的字符串
例程:
    System.out.println( StringUtils.reverse("ABCDE"));
输出如下:
    EDCBA

9.判断字符串内容的类型
函数介绍:
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

10.取得某字符串在另一字符串中出现的次数
使用函数:StringUtils.countMatches(testString,seqString)
函数介绍:取得seqString在testString中出现的次数,未发现则返回零
例程:
    System.out.println(StringUtils.countMatches( "Chinese People", "e"
));
输出:
    4

11.部分截取字符串
使用函数:
StringUtils.substringBetween(testString,fromString,toString ):取得两字符
之间的字符串
StringUtils.substringAfter( ):取得指定字符串后的字符串
StringUtils.substringBefore( ):取得指定字符串之前的字符串
StringUtils.substringBeforeLast( ):取得最后一个指定字符串之前的字符串
StringUtils.substringAfterLast( ):取得最后一个指定字符串之后的字符串

函数介绍:上面应该都讲明白了吧。
例程:
    String formatted = " 25 * (30,40) [50,60] | 30";
    System.out.print("N0: " + StringUtils.substringBeforeLast( formatted, "*" ) );
    System.out.print(", N1: " + StringUtils.substringBetween( formatted, "(", "," ) );
    System.out.print(", N2: " + StringUtils.substringBetween( formatted, ",", ")" ) );
    System.out.print(", N3: " + StringUtils.substringBetween( formatted, "[", "," ) );
    System.out.print(", N4: " + StringUtils.substringBetween( formatted, ",", "]" ) );
    System.out.print(", N5: " + StringUtils.substringAfterLast( formatted, "|" ) );
输出如下:
    N0:  25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5:  30

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet.class org.apache.commons.lang.CharSetUtils.class org.apache.commons.lang.CharUtils.class org.apache.commons.lang.Clas sUtils.class org.apache.commons.lang.Entities$ArrayEntityMap.class org.apache.commons.lang.Entities$BinaryEntityMap.class org.apache.commons.lang.Entities$EntityMap.class org.apache.commons.lang.Entities$HashEntityMap.class org.apache.commons.lang.Entities$LookupEntityMap.class org.apache.commons.lang.Entities$MapIntMap.class org.apache.commons.lang.Entities$PrimitiveEntityMap.class org.apache.commons.lang.Entities$TreeEntityMap.class org.apache.commons.lang.Entities.class org.apache.commons.lang.IllegalClassException.class org.apache.commons.lang.IncompleteArgumentException.class org.apache.commons.lang.IntHashMap$Entry.class org.apache.commons.lang.IntHashMap.class org.apache.commons.lang.LocaleUtils.class org.apache.commons.lang.NotImplementedException.class org.apache.commons.lang.NullArgumentException.class org.apache.commons.lang.NumberRange.class org.apache.commons.lang.NumberUtils.class org.apache.commons.lang.ObjectUtils$Null.class org.apache.commons.lang.ObjectUtils.class org.apache.commons.lang.RandomStringUtils.class org.apache.commons.lang.SerializationException.class org.apache.commons.lang.SerializationUtils.class org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache.commons.lang.Validate.class org.apache.commons.lang.WordUtils.class org.apache.commons.lang.builder.CompareToBuilder.class org.apache.commons.lang.builder.EqualsBuilder.class org.apache.commons.lang.builder.HashCodeBuilder.class org.apache.commons.lang.builder.ReflectionToStringBuilder$1.class org.apache.commons.lang.builder.ReflectionToStringBuilder.class org.apache.commons.lang.builder.StandardToStringStyle.class org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$NoFieldNameToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$ShortPrefixToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$SimpleToStringStyle.class org.apache.commons.lang.builder.ToStringStyle.class org.apache.commons.lang.enum.Enum$Entry.class org.apache.commons.lang.enum.Enum.class org.apache.commons.lang.enum.EnumUtils.class org.apache.commons.lang.enum.ValuedEnum.class org.apache.commons.lang.enums.Enum$Entry.class org.apache.commons.lang.enums.Enum.class org.apache.commons.lang.enums.EnumUtils.class org.apache.commons.lang.enums.ValuedEnum.class org.apache.commons.lang.exception.ExceptionUtils.class org.apache.commons.lang.exception.Nestable.class org.apache.commons.lang.exception.NestableDelegate.class org.apache.commons.lang.exception.NestableError.class org.apache.commons.lang.exception.NestableException.class org.apache.commons.lang.exception.NestableRuntimeException.class org.apache.commons.lang.math.DoubleRange.class org.apache.commons.lang.math.FloatRange.class org.apache.commons.lang.math.Fraction.class org.apache.commons.lang.math.IntRange.class org.apache.commons.lang.math.JVMRandom.class org.apache.commons.lang.math.LongRange.class org.apache.commons.lang.math.NumberRange.class org.apache.commons.lang.math.NumberUtils.class org.apache.commons.lang.math.RandomUtils.class org.apache.commons.lang.math.Range.class org.apache.commons.lang.mutable.Mutable.class org.apache.commons.lang.mutable.MutableBoolean.class org.apache.commons.lang.mutable.MutableByte.class org.apache.commons.lang.mutable.MutableDouble.class org.apache.commons.lang.mutable.MutableFloat.class org.apache.commons.lang.mutable.MutableInt.class org.apache.commons.lang.mutable.MutableLong.class org.apache.commons.lang.mutable.MutableObject.class org.apache.commons.lang.mutable.MutableShort.class org.apache.commons.lang.text.CompositeFormat.class org.apache.commons.lang.text.StrBuilder$StrBuilderReader.class org.apache.commons.lang.text.StrBuilder$StrBuilderTokenizer.class org.apache.commons.lang.text.StrBuilder$StrBuilderWriter.class org.apache.commons.lang.text.StrBuilder.class org.apache.commons.lang.text.StrLookup$MapStrLookup.class org.apache.commons.lang.text.StrLookup.class org.apache.commons.lang.text.StrMatcher$CharMatcher.class org.apache.commons.lang.text.StrMatcher$CharSetMatcher.class org.apache.commons.lang.text.StrMatcher$NoMatcher.class org.apache.commons.lang.text.StrMatcher$StringMatcher.class org.apache.commons.lang.text.StrMatcher$TrimMatcher.class org.apache.commons.lang.text.StrMatcher.class org.apache.commons.lang.text.StrSubstitutor.class org.apache.commons.lang.text.StrTokenizer.class org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time.DurationFormatUtils$Token.class org.apache.commons.lang.time.DurationFormatUtils.class org.apache.commons.lang.time.FastDateFormat$CharacterLiteral.class org.apache.commons.lang.time.FastDateFormat$NumberRule.class org.apache.commons.lang.time.FastDateFormat$PaddedNumberField.class org.apache.commons.lang.time.FastDateFormat$Pair.class org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons.lang.time.FastDateFormat$TimeZoneDisplayKey.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNameRule.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNumberRule.class org.apache.commons.lang.time.FastDateFormat$TwelveHourField.class org.apache.commons.lang.time.FastDateFormat$TwentyFourHourField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitMonthField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitNumberField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitYearField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedMonthField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedNumberField.class org.apache.commons.lang.time.FastDateFormat.class org.apache.commons.lang.time.StopWatch.class
### 回答1: StringUtils.isBlank()是一个Apache Commons Lang库中的方法,用于判定某个字符串是否为空或长度为0或只有空白符的字符串。一般用来判断字符串是否为空或者null。如果字符串被视为空,则返回true,否则返回false。如果字符串是空或null,则表示该字符串没有任何可见或可打印的字符。 ### 回答2: StringUtils.isBlank是一个Java工具类中的一个方法,它用于判断一个字符串是否为空或者只包含空格。其使用方法如下: 1. 导入StringUtils类: 在使用StringUtils.isBlank方法之前,首先需要导入StringUtils类。可以使用import语句导入该类,例如: ``` import org.apache.commons.lang3.StringUtils; ``` 2. 使用StringUtils.isBlank方法判断字符串是否为空或者只包含空格: StringUtils.isBlank方法接收一个字符串作为参数,并返回一个boolean值。如果该字符串为空或者只包含空格,则返回true;否则返回false。例如: ``` String str = " "; boolean result = StringUtils.isBlank(str); System.out.println(result); // 输出true ``` 3. StringUtils.isBlank与其他字符串判断方法的区别: - StringUtils.isBlank方法与String.isEmpty方法的区别在于,StringUtils.isBlank方法在判断字符串为空时还会将连续的空格视为字符串为空。而String.isEmpty方法只判断字符串是否为空,不会将连续的空格视为空字符串。 - StringUtils.isBlank方法与String.trim方法的区别在于,StringUtils.isBlank方法不仅会将字符串两端的空格去除,还会将字符串中间的空格视为字符串为空。而String.trim方法只会去除字符串两端的空格,不会将字符串中间的空格视为空字符串。 总之,StringUtils.isBlank方法是一个方便的工具方法,用于判断字符串是否为空或者只包含空格。在实际开发中,我们可以使用StringUtils.isBlank方法来判断用户输入的字符串是否符合要求,从而进行相应的处理。 ### 回答3: StringUtils.isBlank是一个工具类,它位于Apache Commons Lang库中。它的作用是判断一个字符串是否为空或者只包含空格。该方法会忽略字符串中的制表符、换行符和其他空白字符。 使用StringUtils.isBlank的语法非常简单,只需要将需要判断的字符串作为参数传递给该方法即可。以下是一个示例: ```java String str1 = ""; String str2 = " "; String str3 = "Hello"; String str4 = null; boolean isBlank1 = StringUtils.isBlank(str1); // true boolean isBlank2 = StringUtils.isBlank(str2); // true boolean isBlank3 = StringUtils.isBlank(str3); // false boolean isBlank4 = StringUtils.isBlank(str4); // true ``` 通过以上代码,我们可以看到StringUtils.isBlank会根据传入的字符串判断其是否为空或者只包含空格,并返回对应的boolean值。当字符串为空、只包含空格或者为null时,返回true;否则返回false。 该方法在实际开发中非常实用,可以帮助我们快速判断一个字符串的内容,以便进行后续的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值