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); // zzzzcdef
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. StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)

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

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

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

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

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

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

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

  5. StringUtils工具类常用方法

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

  6. 利用StringUtils工具类进行String为空的判断

      利用工具类进行String类型数据的非空判断,让自己的项目代码变得更加的简洁明了.   判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0   下面是 St ...

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

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

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

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

  9. 基于StringUtils工具类的常用方法介绍(必看篇)

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

随机推荐

  1. Kylin如何进行JDBC方式访问或者调用

    Kylin提供了标准的ODBC和JDBC接口,能够和传统BI工具进行很好的集成.分析师们可以用他们最熟悉的工具来享受Kylin带来的快速.我们也可以对它进行定制开发报表等,把kylin当做数据库服务器 ...

  2. J - Printer Queue 优先队列与队列

    来源poj3125 The only printer in the computer science students' union is experiencing an extremely heav ...

  3. Butterknife 导入项目配置

    在app的 build.gradle 文件中添加 dependencies { // Butterknifeapi 'com.jakewharton:butterknife:8.6.0'annotat ...

  4. Javascript 异步处理与计时跳转

    实现计时跳转的代码: <html lang="en"> <head> <meta charset="UTF-8"> < ...

  5. python开发 *进程数据隔离.守护进程,进程同步工具 * 180725

    进程数据隔离.守护进程,进程同步工具 一.进程之间的数据隔离: from multiprocessing import Process n=100 #主程序中变量n= def func(): glob ...

  6. 求最短路的三种方法:dijkstra,spfa,floyd

    dijkstra是一种单源最短路算法.在没有负权值的图上,vi..vj..vk是vi到vk最短路的话,一定要走vi到vj的最短路.所以每次取出到起点距离最小的点,从该点出发更新邻接的点的距离,如果更新 ...

  7. jquery异步ajax超大长度base64图片长字段数据传输问题解决办法和php后台处理办法

    2017年5月9日19:25:02 在做在线签名的时候,到了图片上传的时候,使用jquery异步ajax上传base64的图片数据的时候,使用默认的方式进行数据传输偶尔会出现 生产的图片只有上半部分, ...

  8. [ssh] 通过ssh私钥生成公钥的方法

    ssh-keygen -y -f .ssh/id_rsa.key id_rsa.key是私钥.

  9. mybatis入门--配置

    1.导入jar包 mybatis-x.x.x.jar 导入到lib目录下, 如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中: <depen ...

  10. FQ原理

    笔者在nginx反向代理篇讲了正向代理和反向代理的区别,今天着重讲其中的FQ是实现原理. 一.普遍的两种方式 1.vpn vpn它将客户端的IP数据报经过加密和二次封装后转发出去,客户端通过vpn上网 ...