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

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

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

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

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

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

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

  1. StringUtils.isBlank(""); // true
  2. StringUtils.isBlank(" "); // true
  3. StringUtils.isBlank(" "); // true
  4. StringUtils.isBlank("\t"); // true
  5. StringUtils.isBlank("\r"); // true
  6. StringUtils.isBlank("\n"); // true
  7. StringUtils.isBlank(null); // true
  8.  
  9. StringUtils.isEmpty(""); // true
  10. StringUtils.isEmpty(" "); // false
  11. StringUtils.isEmpty(" "); // false
  12. StringUtils.isEmpty("\t"); // false
  13. StringUtils.isEmpty("\r"); // false
  14. StringUtils.isEmpty("\n"); // false
  15. StringUtils.isEmpty(null); // true
  16.  
  17. StringUtils.isWhitespace(""); // true
  18. StringUtils.isWhitespace(" "); // true
  19. StringUtils.isWhitespace(" "); // true
  20. StringUtils.isWhitespace("\t"); // true
  21. StringUtils.isWhitespace("\r"); // true
  22. StringUtils.isWhitespace("\n"); // true
  23. 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,具体参见下面的例子:

  1. StringUtils.isAnyBlank("titanic", "jack", "rose")); // false
  2. StringUtils.isAnyBlank("", "jack", "rose")); // true
  3. StringUtils.isAnyBlank(" ", "jack", "rose")); // true
  4. StringUtils.isAnyBlank(null, "jack", "rose")); // true
  5.  
  6. StringUtils.isAnyEmpty("titanic", "jack", "rose")); // false
  7. StringUtils.isAnyEmpty("", "jack", "rose")); // true
  8. StringUtils.isAnyEmpty(" ", "jack", "rose")); // false
  9. StringUtils.isAnyEmpty(null, "jack", "rose")); // true
  10.  
  11. StringUtils.isNoneBlank("titanic", "jack", "rose")); // true
  12. StringUtils.isNoneBlank("", "jack", "rose")); // false
  13. StringUtils.isNoneBlank(" ", "jack", "rose")); // false
  14. StringUtils.isNoneBlank(null, "jack", "rose")); // false
  15.  
  16. StringUtils.isNoneEmpty("titanic", "jack", "rose")); // true
  17. StringUtils.isNoneEmpty("", "jack", "rose")); // false
  18. StringUtils.isNoneEmpty(" ", "jack", "rose")); // true
  19. StringUtils.isNoneEmpty(null, "jack", "rose")); // false

二、转换

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

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

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

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

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

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

  (3)字符串大小写互换

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

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

  1. StringUtils.isAllUpperCase(null)); // false
  2. StringUtils.isAllUpperCase("")); // false
  3. StringUtils.isAllUpperCase(" ")); // false
  4. StringUtils.isAllUpperCase("CHINA")); // true
  5. StringUtils.isAllLowerCase(null)); // false
  6. StringUtils.isAllLowerCase("")); // false
  7. StringUtils.isAllLowerCase(" ")); // false
  8. StringUtils.isAllLowerCase("china")); // true

三、移除

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

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

  (1)移除单个字符

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

  (2)移除指定字符序列

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

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

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

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

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

  (5)移除空白字符

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

四、替换

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

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

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

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

    (a)replace方法

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

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

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

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

    (b)replaceChars方法

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

  1. StringUtils.replaceChars("china", 'a', 'z')); // chinz
  2. 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作为结束索引

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

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

  1. StringUtils.overlay("abcdef", "zzzz", -1, 2)); // abcdzz
  2. 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

  1. StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z" })); // xhinz (将ch和a分别替换为x和z)
  2. StringUtils.replaceEach("china", null, new String[] { "x", "z" })); // china (存在null,不进行替换)
  3. 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. 1)reverse(String str)
  2. 2)reverseDelimited(String str, char separatorChar)

  (1)简单反转

  reverse(String str)方法

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

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

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

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

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

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

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

      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. Python引用某一文件的方法出现红色波浪线

    from parse import parse_url#引用parse里面的方法 结果出现波浪线并提示 This inspection detects names that should resolv ...

  2. numpy常用函数之arange函数

    2.np.arange([start, ]stop, [step, ]dtype=None) 作用:   arange函数用于创建等差数组 start:可忽略不写,默认从0开始;起始值 stop:结束 ...

  3. GO Range

    Go 语言中 range 关键字用于 for 循环中迭代数组(array).切片(slice).通道(channel)或集合(map)的元素.在数组和切片中它返回元素的索引和索引对应的值,在集合中返回 ...

  4. Euler Sums系列(四)

    \[\Large\displaystyle \sum_{n=1}^\infty (-1)^n \frac{H_n}{2n+1}=\mathbf{G}-\frac{\pi}{2}\ln(2)\] \(\ ...

  5. 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战

    import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...

  6. 杭电2504 又见GCD

    又见GCD Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. 线上学习-语言模型 language model

    chain rule markov assumption 评估语言模型 平滑方法

  8. 安装go和goland

    1.建议去go语言中文网下载,网址:https://studygolang.com/dl ,下图是下载页面及包介绍 2.Windows版安装 3.在cmd命令行窗口输入“go version”可以查看 ...

  9. 研究Zookeeper的原理(二)

    阅读声明:以下内容是结合网上材料及工作内容所写的个人理解,如有不当,欢迎大家指正~~~谢谢啦 一.ZooKeeper的选举机制.FailOver机制 我们知道ZooKeeper在分布式环境中协调服务, ...

  10. wc、grep 、 cut、paste 和 tr 命令的用法

    1 wc 命令 wc 命令是一个统计的工具,主要用来显示文件所包含的行.字和字节数. wc 命令是 word count 的缩写. (1)命令格式 wc [选项] [文件] (2)常用参数 参数 描述 ...