博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
总结StringUtils工具类字符串操作的方法
阅读量:3898 次
发布时间:2019-05-23

本文共 7134 字,大约阅读时间需要 23 分钟。

StringUtils工具类

Apache commons lang包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便。在这里将常用的方法总结了一下.要想使用类必须要先导入commons-lang-X.jar文件到项目中

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

非空判断

StringUtils中非空判断是最常用的,其方法主要有以下几个

  1. boolean StringUtils.isBlank(String str);
  2. boolean StringUtils.isNotBlank(String str);
  3. boolean StringUtils.isEmpty(String str);
  4. boolean StringUtils.isNotEmpty(String str);
  5. boolean StringUtils.isWhitespace(arg0);

isBlankisNotBlank,isEmpty与isNotEmpty是是非关系.通过下面的实例具体来看下三种方法判断非空的区别:

System.out.println(StringUtils.isBlank("")); // true

System.out.println(StringUtils.isBlank(" ")); // true

System.out.println(StringUtils.isBlank("     ")); // true

System.out.println(StringUtils.isBlank("\t")); // true

System.out.println(StringUtils.isBlank("\r")); // true

System.out.println(StringUtils.isBlank("\n")); // true

System.out.println(StringUtils.isBlank(null)); // true

 

System.out.println(StringUtils.isEmpty("")); // true

System.out.println(StringUtils.isEmpty(" ")); // false

System.out.println(StringUtils.isEmpty("     ")); // false

System.out.println(StringUtils.isEmpty("\t")); // false

System.out.println(StringUtils.isEmpty("\r")); // false

System.out.println(StringUtils.isEmpty("\n")); // false

System.out.println(StringUtils.isEmpty(nu ll)); // true

 

System.out.println(StringUtils.isWhitespace("")); // true

System.out.println(StringUtils.isWhitespace(" ")); // true

System.out.println(StringUtils.isWhitespace("    ")); // true

System.out.println(StringUtils.isWhitespace("\t")); // true

System.out.println(StringUtils.isWhitespace("\r")); // true

System.out.println(StringUtils.isWhitespace("\n")); // true

System.out.println(StringUtils.isWhitespace(null)); // false

上面的结果可以看到:

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

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

转换

  1. 字符串首字母大小写转换

// null (注意此处不会报异常)

System.out.println(StringUtils.capitalize(null));  //null

//(首字母转大写)

System.out.println(StringUtils.capitalize("china")); // China System.out.println(StringUtils.uncapitalize(null)); // null  

//(首字母转小写)

System.out.println(StringUtils.uncapitalize("CHINA")); // cHINA  

  1. 字符串整体大小写转换

//(全部转为大写)

System.out.println(StringUtils.upperCase(null)); // null

System.out.println(StringUtils.upperCase("china")); // CHINA

// CHINA (按照指定规则转换为大写)

System.out.println(StringUtils.upperCase("china", Locale.ENGLISH));

//(全部转换为小写)

System.out.println(StringUtils.lowerCase(null)); // null

System.out.println(StringUtils.lowerCase("CHINA")); // china

// china (按照指定转换规则转换为小写)

System.out.println(StringUtils.lowerCase("CHINA", Locale.ENGLISH));

  1. 字符串大小写互换

System.out.println(StringUtils.swapCase(null)); // null

System.out.println(StringUtils.swapCase("chINA")); // CHina

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

System.out.println(StringUtils.isAllUpperCase(null)); // false

System.out.println(StringUtils.isAllUpperCase("")); // false

System.out.println(StringUtils.isAllUpperCase(" ")); // false

System.out.println(StringUtils.isAllUpperCase("CHINA")); // true

System.out.println(StringUtils.isAllLowerCase(null)); // false

System.out.println( StringUtils.isAllLowerCase("")); // false

System.out.println( StringUtils.isAllLowerCase(" ")); // false

System.out.println(StringUtils.isAllLowerCase("china")); // true

移除

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

  1. 移除单个字符

System.out.println(StringUtils.remove(null, 'a')); // null  

System.out.println(StringUtils.remove("china", null)); // china

System.out.println(StringUtils.remove("china", 'i')); // chna

//(如果要移除的字符不存在,则返回原字符串)

System.out.println(StringUtils.remove("china", 'b')); // china

  1. 移除指定字符串

System.out.println(StringUtils.remove("china", "in")); // cha

System.out.println(StringUtils.remove("china", "nin")); // china

  1. 移除开头匹配字符串

System.out.println(StringUtils.removeStart("china", "ch")); // ina

// na (忽略大小写)

System.out.println(StringUtils.removeStartIgnoreCase("china","CHI"));

  1. 移除结尾匹配的字符串

System.out.println(StringUtils.removeEnd("china", "na")); // chi

// chi (忽略大小写)

System.out.println(StringUtils.removeEndIgnoreCase("china", "NA"));

  1. 移除空白字符

System.out.println(StringUtils.deleteWhitespace(null)); //null

System.out.println(StringUtils.deleteWhitespace(" c h  i\tn\ra")); // china

替换

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

  1. 替换单个字符或字符序列
    1. replace方法用于替换单个字符序列
      1. 单个字符序列

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

      1. 指定最大替换的个数

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表示全部替换)

 

    1. replaceChars方法替换单个字符或者单个字符序列

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

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

    1. replaceOnce方法

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

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

    1. 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

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

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

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

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

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

  1. 同时替换多个字符序列
    1. replaceEach方法

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

// xhinz (将ch和a分别替换为x和z)

StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z" }));

// china (存在null,不进行替换)

StringUtils.replaceEach("china", null, new String[] {

"x", "z" }));

// IllegalArgumentException (被替换和替换的个数不对应)

StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z", "y" }));

 

    1. replaceEachRepeatedly方法

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

// zhina (c被替换为x,x又被替换为z)

 StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "z" }));

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

StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "c" }));

// IllegalStateException (c被替换为x,x又被替换为c)

反转

  1. reverse(String str)方法

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

  1. 根据指定分隔符进行反转,分隔符之间的字符不进行反转

  StringUtils.reverseDelimited("china", ',')); // china

  StringUtils.reverseDelimited("cxhinxa", 'x')); // axhinxz

  StringUtils.reverseDelimited("c.hin.a", '.')); // a.hin.c

  StringUtils.reverseDelimited("c.hina", '.')); // hina.c

 

 

 

 

 

转载地址:http://ciyen.baihongyu.com/

你可能感兴趣的文章
vue-cli中实现dolist
查看>>
sass的安装
查看>>
Vue-cli中路由配置
查看>>
豆瓣高分JAVA书籍,你都读过吗?
查看>>
java图书管理系统
查看>>
C#图书管理系统
查看>>
C#酒店管理系统
查看>>
你对ArrayList了解多少?
查看>>
《从Paxos到ZooKeeper分布式一致性原理与实践》学习知识导图
查看>>
Java基础面试题(一) (2020持续更新)
查看>>
JAVA人事管理系统
查看>>
Dubbo面试题(关注小R持续更新)
查看>>
JAVA仿微博系统(JAVA毕业设计含源码和运行教程)
查看>>
24BITBMP位图的文件结构及创建
查看>>
如何在自定义控件中获得width和height?
查看>>
Android UI开发专题之界面设计【基础API】
查看>>
ejarmaker: jar 、java类的加密工具
查看>>
配置NFS实现Linux服务器之间的文件共享
查看>>
PostgreSQL连接池pgbouncer的使用
查看>>
Kryo序列化进阶学习: 加密数据
查看>>