org.apache.commons.lang.StringUtil(StringUtil包函数(用法))

import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.ConvertUtils;import org.apache.commons.beanutils.LazyDynaBean;import org.apache.commons.beanutils.converters.BigDecimalConverter;import org.apache.commons.beanutils.converters.DoubleConverter;import org.apache.commons.beanutils.converters.IntegerConverter;import org.apache.commons.beanutils.converters.LongConverter; import org.apache.commons.lang.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

=========================================================================================================

String sName = "Java转义字符(补遗)";sName = sName.replaceFirst("(补遗)","");out.println(sName);

如果你以为会输出“Java转义字符”,那你就错了,事实上输出“Java转义字符()”,我也很奇怪,以为是中英文括号的问题,可是并不是,我不确定是否转义问题,解决方法是

sName = sName.replaceFirst("\\(补遗\\)","");

StringUtil 简单用法相关推荐

  1. 反编译工具jad简单用法

    反编译工具jad简单用法 下载地址: [url]http://58.251.57.206/down1?cid=B99584EFA6154A13E5C0B273C3876BD4CC8CE672& ...

  2. QCustomPlot的简单用法总结

    QCustomPlot的简单用法总结 第一部分:QCustomPlot的下载与安装 第二部分:QCustomPlot在VS2013+QT下的使用 QCustomPlot的简单用法总结    写在前面, ...

  3. python matplotlib 简单用法

    python matplotlib 简单用法 具体内容请参考官网 代码 import matplotlib.pyplot as plt import numpy as np # 支持中文 plt.rc ...

  4. Windump网络命令的简单用法

    Windump网络命令的简单用法 大家都知道,unix系统下有个tcpdump的抓包工具,非常好用,是做troubleshooting的好帮手.其实在windows下也有一个类似的工作,叫windum ...

  5. Android TabLayout(选项卡布局)简单用法实例分析

    本文实例讲述了Android TabLayout(选项卡布局)简单用法.分享给大家供大家参考,具体如下: 我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合.达到 ...

  6. shell expect的简单用法

    为什么需要expect?     我们通过Shell可以实现简单的控制流功能,如:循环.判断等.但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如 telnet服务器等进 ...

  7. Shellz中awk的简单用法

    其实shell脚本的功能常常被低估.在实际应用中awk sed 等用法可以为shell提供更为强大的功能.下面我们将一下awk调用的简单方法进行了总结.方便同学们学习: awk的简单用法: 第一种调用 ...

  8. python装饰器实例-Python装饰器原理与简单用法实例分析

    本文实例讲述了Python装饰器原理与简单用法.分享给大家供大家参考,具体如下: 今天整理装饰器,内嵌的装饰器.让装饰器带参数等多种形式,非常复杂,让人头疼不已.但是突然间发现了装饰器的奥秘,原来如此 ...

  9. python装饰器实例-Python装饰器简单用法实例小结

    本文总结分析了Python装饰器简单用法.分享给大家供大家参考,具体如下: 装饰器在python中扮演着很重要的作用,例如插入日志等,装饰器可以为添加额外的功能同时又不影响业务函数的功能. 比如,运行 ...

最新文章

  1. 《Android App开发入门:使用Android Studio 2.X开发环境》——1-3 Android Studio 快速上手...
  2. linux/CentOS 6忘记root密码解决办法
  3. excel if in函数_EXCEL技巧之if函数在统计中的应用简介
  4. 【任务脚本】0601更新autojs客户端,回顾之前战绩,注意事项淘宝618活动领喵币autojs脚本,向大神致敬...
  5. DIV层跟随鼠标位置显示提示
  6. python函数(二)
  7. SqlServer学习之触发器
  8. Netweaver里某个software component和C4C的版本
  9. 57 MM配置-评估和科目设置-物料类型与账户分类参考对应关系
  10. 联想y50更换固态硬盘_联想y50怎么加固态硬盘而不换原来的机器硬盘?
  11. 印象笔记使用方法————印象笔记下载安装以及浏览器插件下载安装
  12. 微信公众号网页授权登录
  13. 2018第九届蓝桥杯国赛C组_java
  14. 小程序外链,实现桌面图标、短信和邮件外链跳转到小程序的工具
  15. 计算机B和D的转换,模数转换
  16. 微信小程序(小游戏)的示例
  17. 浅谈计算机应用的认识,浅谈《计算机应用基础》教学
  18. 600道计算机二级python选择题在线真题题库
  19. 香港服务器几种网站测速的方法
  20. 昭阳区计算机学校,昭阳区高级职业中学

热门文章

  1. HapMap-人类基因组单倍型图谱
  2. Javascript 实现一个分钟秒钟倒计时器
  3. Python爬取租房数据实例,据说可以入门爬虫的小案例!
  4. 笔记本每次启动都会先黑屏1分钟再启动问题解决
  5. 云原生(CloudNative)将成为应用云化开发的主流方式
  6. tensorflow实现卷积神经网络——经典网络(LeNet5、AlexNet、VGG-16、ResNet)
  7. sqlserver2008 服务 连接失败 -服务器名称如何写!!
  8. linux上ftp登录失败解决办法
  9. opencv2读取摄像头并保存为视频
  10. 腾讯47岁T13大佬被裁,厂龄15年依然被毕业?