设为首页 收藏本站
查看: 786|回复: 0

[经验分享] org.apache.commons.lang.StringUtil 常用方法使用例子

[复制链接]

尚未签到

发表于 2017-1-12 07:22:15 | 显示全部楼层 |阅读模式
  1.空字符串检查
使用函数: StringUtils.isBlank(testString)
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False
例程:
view plaincopy to clipboardprint?


  • 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 ) );  

      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
例程:
view plaincopy to clipboardprint?


  • 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 ) );  

      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则返回原字符串.
例程:
view plaincopy to clipboardprint?


  • 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 ) );  

      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和劈分字符串间有抵触关系,建议一般情况下
不要设定长度.
例程:
view plaincopy to clipboardprint?


  • 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 ) );  

      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之间的字符串。不存在则返回空
例程:
view plaincopy to clipboardprint?


  • String htmlContent = "ABC1234ABC4567";   
  • System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));   
  • System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567"));  

      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尾部的换行符
例程:
view plaincopy to clipboardprint?


  • String input = "Hello\n";   
  • System.out.println( StringUtils.chomp( input ));   
  • String input2 = "Another test\r\n";   
  • System.out.println( StringUtils.chomp( input2 ));  

      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次后的字符串
例程:
view plaincopy to clipboardprint?


  • System.out.println( StringUtils.repeat( "*", 10));   
  • System.out.println( StringUtils.repeat( "China ", 5));  

      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
例程:
view plaincopy to clipboardprint?


  • System.out.println( StringUtils.center( "China", 11,"*"));  

      System.out.println( StringUtils.center( "China", 11,"*"));
输出如下:
    ***China***


8.颠倒字符串
使用函数:StringUtils.reverse(testString)
函数介绍:得到testString中字符颠倒后的字符串
例程:
view plaincopy to clipboardprint?


  • System.out.println( StringUtils.reverse("ABCDE"));  

      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

例程:
view plaincopy to clipboardprint?


  • 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 ) );  

      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中出现的次数,未发现则返回零
例程:
view plaincopy to clipboardprint?


  • System.out.println(StringUtils.countMatches( "Chinese People", "e"));  

      System.out.println(StringUtils.countMatches( "Chinese People", "e"));
输出:
    4

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

函数介绍:上面应该都讲明白了吧。
例程:
view plaincopy to clipboardprint?


  • 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, "|" ) );  

      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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-327141-1-1.html 上篇帖子: apache+tomcat6.0集群 负载均衡配置文档 下篇帖子: apache poi 导入2007excel 及处理excel不可读取内容!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表