Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便。最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总结了一下,方便以后查阅,大家有兴趣也可以看一下。

  首先需要说明的一点是,StringUtils类在操作字符串时,即使操作的为null值也是安全的,不会报NullPointerException,这一点在后面的例子中再具体说明。因此,在操作字符串时使用StringUtils相比使用原生的String会更加安全。

一、判空(这应该是最常用的)###

  StringUtils中判断字符串是否为空的方法主要有以下几个:

	1)boolean StringUtils.isBlank(String str)
2)boolean StringUtils.isEmpty(String str)
3)boolean StringUtils.isNotBlank(String str)
4)boolean StringUtils.isNotEmpty(String str)
5)boolean StringUtils.isAnyBlank(CharSequence… css)
6)boolean StringUtils.isAnyEmpty(CharSequence… css)
7)boolean StringUtils.isNoneBlank(CharSequence… css)
8)boolean StringUtils.isNoneEmpty(CharSequence… css)
9)boolean StringUtils.isWhitespace(CharSequence cs)

  很显然上面的方法很多都是是非的关系,isBlank与isNotBlank,isEmpty与isNotEmpty,isAnyBlank与isNoneBlank,isAnyEmpty与isNoneEmpty都是如此。

  但是blank与empty以及whitesapce它们之间有何区别呢?看下面的对比就很清楚了:

    StringUtils.isBlank(""); // true
StringUtils.isBlank(" "); // true
StringUtils.isBlank(" "); // true
StringUtils.isBlank("\t"); // true
StringUtils.isBlank("\r"); // true
StringUtils.isBlank("\n"); // true
StringUtils.isBlank(null); // true StringUtils.isEmpty(""); // true
StringUtils.isEmpty(" "); // false
StringUtils.isEmpty(" "); // false
StringUtils.isEmpty("\t"); // false
StringUtils.isEmpty("\r"); // false
StringUtils.isEmpty("\n"); // false
StringUtils.isEmpty(null); // true StringUtils.isWhitespace(""); // true
StringUtils.isWhitespace(" "); // true
StringUtils.isWhitespace(" "); // true
StringUtils.isWhitespace("\t"); // true
StringUtils.isWhitespace("\r"); // true
StringUtils.isWhitespace("\n"); // true
StringUtils.isWhitespace(null); // false

  从上面的结果可以看出,

    blank:代表的是空串("")、空白符(空格""," ",制表符"\t",回车符"\r","\n"等)以及null值;

    empty:代表的是空串("")和null值,不包含空白符;

    whitespace:包含空串("")和空白符,不包含null值.

  isBlank,isNotBlank,isEmpty,isNotEmpty四个方法都是用于判断单个字符串是否为空,这个可以参见上面的几个例子。

  isAnyBlank,isNoneBlank,isAnyEmpty,isNoneEmpty四个方法是用于判断多个字符串是否为空;

  对于isAnyBlank和isAnyEmpty来说,只要有一个字符串为空,结果即为true;

对于isNoneBlank和isNoneEmpty,只要存在一个字符串为空,结果即为false,具体参见下面的例子:

    StringUtils.isAnyBlank("titanic", "jack", "rose")); // false
StringUtils.isAnyBlank("", "jack", "rose")); // true
StringUtils.isAnyBlank(" ", "jack", "rose")); // true
StringUtils.isAnyBlank(null, "jack", "rose")); // true StringUtils.isAnyEmpty("titanic", "jack", "rose")); // false
StringUtils.isAnyEmpty("", "jack", "rose")); // true
StringUtils.isAnyEmpty(" ", "jack", "rose")); // false
StringUtils.isAnyEmpty(null, "jack", "rose")); // true StringUtils.isNoneBlank("titanic", "jack", "rose")); // true
StringUtils.isNoneBlank("", "jack", "rose")); // false
StringUtils.isNoneBlank(" ", "jack", "rose")); // false
StringUtils.isNoneBlank(null, "jack", "rose")); // false StringUtils.isNoneEmpty("titanic", "jack", "rose")); // true
StringUtils.isNoneEmpty("", "jack", "rose")); // false
StringUtils.isNoneEmpty(" ", "jack", "rose")); // true
StringUtils.isNoneEmpty(null, "jack", "rose")); // false

二、转换###

  StringUtils中涉及大小写转换以及判断字符串大小写的方法如下:

    1)StringUtils.capitalize(String str)
2)StringUtils.uncapitalize(String str)
3)StringUtils.upperCase(String str)
4)StringUtils.upperCase(String str,Locale locale)
5)StringUtils.lowerCase(String str)
6)StringUtils.lowerCase(String str,Locale locale)
7)StringUtils.swapCase(String str)
8)StringUtils.isAllUpperCase(CharSequence cs)
9)StringUtils.isAllLowerCase(CharSequence cs)

  (1)字符串首字母大小写转换

	StringUtils.capitalize(null)); // null (注意此处不会报异常)
StringUtils.capitalize("china")); // China (首字母转大写)
StringUtils.uncapitalize(null)); // null
StringUtils.uncapitalize("CHINA")); // cHINA (首字母转小写)

  (2)字符串整体大小写转换

    StringUtils.upperCase(null)); // null
StringUtils.upperCase("china")); // CHINA (全部转为大写)
StringUtils.upperCase("china", Locale.ENGLISH)); // CHINA (按照指定规则转换为大写)
StringUtils.lowerCase(null)); // null
StringUtils.lowerCase("CHINA")); // china (全部转换为小写)
StringUtils.lowerCase("CHINA", Locale.ENGLISH)); // china (按照指定转换规则转换为小写)

  (3)字符串大小写互换

    StringUtils.swapCase(null)); // null
StringUtils.swapCase("chINA")); // CHina

  (4)判断字符串是否全部是大写或小写(空或空白符均为false)

    StringUtils.isAllUpperCase(null)); // false
StringUtils.isAllUpperCase("")); // false
StringUtils.isAllUpperCase(" ")); // false
StringUtils.isAllUpperCase("CHINA")); // true
StringUtils.isAllLowerCase(null)); // false
StringUtils.isAllLowerCase("")); // false
StringUtils.isAllLowerCase(" ")); // false
StringUtils.isAllLowerCase("china")); // true

三、移除###

  从字符串中移除匹配的字符或字符序列,如果要移除的字符或字符序列在字符串中不存在,即无匹配,则不进行移除

	1)StringUtils.remove(String str, char remove)
2)StringUtils.remove(String str, String remove)
3)StringUtils.removeStart(String str, String remove)
4)StringUtils.removeStartIgnoreCase(String str, String remove)
5)StringUtils.removeEnd(String str, String remove)
6)StringUtils.removeEndIgnoreCase(String str, String remove)
7)StringUtils.deleteWhitespace(String str)

  (1)移除单个字符

	StringUtils.remove(null, 'a')); // null (注意此处及下一行为null)
StringUtils.remove('china', null) // china
StringUtils.remove("china", 'i')); // chna
StringUtils.remove("china", 'b')); // china (如果要移除的字符不存在,则返回原字符串)

  (2)移除指定字符序列

	StringUtils.remove("china", "in")); // cha
StringUtils.remove("china", "nin")); // china

  (3)移除开头匹配的字符序列

	StringUtils.removeStart("china", "ch")); // ina
StringUtils.removeStartIgnoreCase("china", "CHI")); // na (忽略大小写)

  (4)移除结尾匹配的字符序列

	StringUtils.removeEnd("china", "na")); // chi
StringUtils.removeEndIgnoreCase("china", "NA")); // chi (忽略大小写)

  (5)移除空白字符

	StringUtils.deleteWhitespace(null)); //null
StringUtils.deleteWhitespace(" c h i\tn\ra")); // china

四、替换###

  StringUtils中常用的替换方法有如下几种

	1)replace(String text, String searchString, String replacement)
2)replace(String text, String searchString, String replacement, int max)
3)replaceChars(String str, char searchChar, char replaceChar)
4)replaceChars(String str, String searchChars, String replaceChars)
5)replaceOnce(String text, String searchString, String replacement)
6)overlay(String str,String overlay,int start,int end)
7)replaceEach(String text, String[] searchList, String[] replacementList)
8)replaceEachRepeatedly(String text, String[] searchList, String[]replacementList)

  需要注意的是,若被替换的字符串为null,或者被替换的字符或字符序列为null,又或者替换的字符或字符序列为null,那么此次替换都会被忽略,返回原字符串

  (1)替换单个字符或字符序列

    (a)replace方法

      replace方法可以替换单个字符序列

		StringUtils.replace("china", null, "z")); // china (此处被替换字符序列为null,因此替换会被忽略,返回原字符串)
StringUtils.replace("china", "c", null)); // china (此处替换字符序列为null,因此替换也被忽略,返回原字符串)
StringUtils.replace("china", "a", "ese")); // chinese
StringUtils.replace("china", "a", "")); // chin

      replace方法还可以指定最大替换的个数

		StringUtils.replace("aabaaa", "a", "z", 0)); // aabaaa (0表示替换的个数为0,也就是不替换)
StringUtils.replace("aabaaa", "a", "z", 1)); // zabaaa (1表示最多替换1个)
StringUtils.replace("aabaaa", "a", "z", 2)); // zzbaaa (2表示最多替换2个)
StringUtils.replace("aabaaa", "a", "z", 3)); // zzbzaa (3表示最多替换3个)
StringUtils.replace("aabaaa", "a", "z", -1)); // zzbzaa (-1表示全部替换)

    (b)replaceChars方法

      replaceChars方法可以替换单个字符或者单个字符序列

		StringUtils.replaceChars("china", 'a', 'z')); // chinz
StringUtils.replaceChars("china", "a", "z")); // chinz

    (c)replaceOnce方法

      replaceOnce方法只会替换一次,也就是只会替换第一个要替换的字符序列,后面即使有匹配的字符序列也不会被替换

		StringUtils.replaceOnce("abaa", "a", "z")); // zbaa

    (d)overlay方法

      overlay(String str,String overlay,int start,int end)方法可以在指定位置进行字符序列替换,从start索引处开始(包含)到end-1索引处为止进行替换

         StringUtils.overlay("abcdef", "zzzz", 2, 4)); // abzzzzef

      这里有一些特殊情况:

      1)起始索引start小于结束索引end,这时会将end作为起始索引,start作为结束索引

         StringUtils.overlay("abcdef", "zzzz", 4, 2)); // abzzzzef
StringUtils.overlay("abcdef", "zzzz", 4, 3)); // abczzzzef
StringUtils.overlay("abcdef", "zzzz", 4, 4)); // abcdzzzzef
StringUtils.overlay("abcdef", "zzzz", 4, 5)); // abcdzzzzf

      2)起始索引start为负数,这时start会被当作0处理

         StringUtils.overlay("abcdef", "zzzz", -1, 2)); // abcdzz
StringUtils.overlay("abcdef", "zzzz", -2, -3)); // zzzzabcdef

      3)结束索引end大于原始字符串的长度,这时end会被当作原始字符串长度处理

         StringUtils.overlay("abcdef", "zzzz", 8, 10)); // abcdefzzzz

  (2)同时替换多个字符序列

    (a)replaceEach方法

      replaceEach(String text, String[] searchList, String[] replacementList)方法可以同时替换多个字符序列,但被替换和替换的字符序列的个数应该对应,否则会报IllegalArgumentException

         StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z" })); // xhinz (将ch和a分别替换为x和z)
StringUtils.replaceEach("china", null, new String[] { "x", "z" })); // china (存在null,不进行替换)
StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z", "y" })); // IllegalArgumentException (被替换和替换的个数不对应)

    (b)replaceEachRepeatedly方法

      replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)方法可以循环进行替换,具体见下面的例子:

           StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "z" })); // zhina (c被替换为x,x又被替换为z)

      但如果替换是一个死循环,则会报IllegalStateException:

           StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "c" })); // IllegalStateException (c被替换为x,x又被替换为c)

五、反转###

  StringUtils中有关反转的方法如下:

	1)reverse(String str)
2)reverseDelimited(String str, char separatorChar)

  (1)简单反转

  reverse(String str)方法

    StringUtils.reverse("china")); // anihc

  (2)根据指定分隔符进行反转,分隔符之间的字符不进行反转

    StringUtils.reverseDelimited("china", ',')); // china
StringUtils.reverseDelimited("cxhinxa", 'x')); // axhinxz
StringUtils.reverseDelimited("c.hin.a", '.')); // a.hin.c
StringUtils.reverseDelimited("c.hina", '.')); // hina.c

StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)的更多相关文章

  1. StringUtils工具类常用方法汇总:判空、转换、移除、替换、反转。

    Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...

  2. StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)

      在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取   StringUtils ...

  3. StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)

    Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...

  4. StringUtils工具类常用方法汇总(截取、去除空白、包含、查询索引)

    一.截取   StringUtils中常用的截取字符串的方法如下: substring(String str,int start) substring(String str,int start, in ...

  5. 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空

    通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...

  6. StringUtils工具类常用方法

    前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...

  7. String工具类之“四个判空方式”StringUtils.isNotBlank和StringUtils.isEmpty和StringUtils.isBlank和StringUtils.isNotEmpty

    一.判断str字符串都不为空==>StringUtils.isNotBlank(String str); 1 /** 2 * <p>检查一个字符串是否非空("") ...

  8. StringUtils工具类常用方法详解

    StringUtils 常用方法 1.isEmpty(String str) 是否为空,空格字符为false2.isNotEmpty(String str) 是否为非空,空格字符为true3.isBl ...

  9. StringUtils工具类常用方法介绍(持续更新)

    StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出Nu ...

随机推荐

  1. vs2012建一个空解决方案添加以前老版本的Web项目调试弹出window安全

    解决方法:NTLM身份验证去掉就行.

  2. 【转】ATA Secure Erase

    ATA Secure Erase     This procedure describes how to use the hdparm command to issue a Secure Erase  ...

  3. 一个有意思的Python小程序(全国省会名称随机出题)

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近比较迷Python,仿照<Python编程快速上手>8.5写了一个随机出卷的小 ...

  4. 2_ROS学习

    2_VNC远程连接树莓派 在上一次,我们成功的给树莓派安装了Ubuntu mate的操作系统. 树莓派是嵌入式计算机,一般是没有显示屏来显示的,我们通过远程连接来访问树莓派.网上推荐了ssh连接,xr ...

  5. hibernate的session详解

  6. Hdu 1698(线段树 区间修改 区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  7. Problem D

    Problem Description An entropy encoder is a data encoding method that achieves lossless data compres ...

  8. 使用ichartjs生成图表

    官网:http://www.ichartjs.com/ ichartjs 是一款基于HTML5的图形库.使用纯javascript语言, 利用HTML5的canvas标签绘制各式图形. ichartj ...

  9. ssh分发秘钥时出现错误“Permission denied (publickey,gssapi-keyex,gssapi-with-mic)”

    因为公司的服务器连接是通过xshell公钥和密码连接的,今天在ssh分发秘钥的时候出现了,下面的错误: [root@iZ2ze97cumk8opqm28h8Z .ssh]# ssh-copy-id - ...

  10. C# into子句

    可使用 into 上下文关键字创建临时标识符,将 group.join 或 select 子句的结果存储至新标识符. 此标识符本身可以是附加查询命令的生成器. 有时称在 group 或 select  ...