StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
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工具类常用方法汇总(判空、转换、移除、替换、反转)的更多相关文章
- StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方 ...
- StringUtils工具类常用方法汇总:判空、转换、移除、替换、反转。
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取 StringUtils ...
- StringUtils工具类常用方法汇总(截取、去除空白、包含、查询索引)
一.截取 StringUtils中常用的截取字符串的方法如下: substring(String str,int start) substring(String str,int start, in ...
- StringUtils工具类常用方法
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- 利用StringUtils工具类进行String为空的判断
利用工具类进行String类型数据的非空判断,让自己的项目代码变得更加的简洁明了. 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 下面是 St ...
- StringUtils工具类常用方法详解
StringUtils 常用方法 1.isEmpty(String str) 是否为空,空格字符为false2.isNotEmpty(String str) 是否为非空,空格字符为true3.isBl ...
- StringUtils工具类常用方法介绍(持续更新)
StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出Nu ...
- 基于StringUtils工具类的常用方法介绍(必看篇)
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
随机推荐
- MySQL 分页数据错乱重复
select xx from table_name wheere xxx order by 字段A limit offset;, 表数据总共 48 条,分页数量正常,但出现了结果混杂的情况,第一页的数 ...
- 使用ReentrantLock和Condition来代替内置锁和wait(),notify(),notifyAll()
使用ReentrantLock可以替代内置锁,当使用内置锁的时候,我们可以使用wait() nitify()和notifyAll()来控制线程之间的协作,那么,当我们使用ReentrantLock的时 ...
- OpenJDK和JDK区别
OpenJDK和JDK区别 OpenJDK与JDK的区别分析 Sun的JDK7.OpenJDK及IcedTea释疑 简介(ps): 简单来说jdk从7开始,弄出一个可以自由使用的公共版本(openjd ...
- python中的张量运算(tensor)
1,首先比较二者的参数部分:这就是处理0阶张量和1阶张量的区别 np.max:(a, axis=None, out=None, keepdims=False) 求序列的最值 最少接收一个参数 axis ...
- 第三天 Linux简单命令
2018-5-22 15:21:59 使用 atom 可以在windows环境下同步代码与linux (汉化配置好就可以啦) 2018-4-13 18:09:31 该看32节啦 1.man +陌生命 ...
- laravel5.4+vue+element简单搭建(gulp+laravel Elixir)(转)
如今laravel来到5.4版本,更方便引入vue了,具体步骤如下: 下图为我动到的文件 1.下载laravel5.4 2.命令行(laravel5.4目录下):composer install 3. ...
- ping不通,配置dns
vim /etc/resolv.conf nameserver 119.29.29.29 nameserver 182.254.116.116 nameserver 8.8.8.8
- Delphi 中的 XMLDocument 类详解(10) - 判断节点类型: 支节点、叶节点、文本节点、空节点
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- 19.1-uC/OS-III内存管理应用
一个处理器,在不断地分配和释放内存的过程中,一整块连续的内存被分散为很多离散的小块内存, 这些叫做内存碎片, 内存碎片过多会导致内存的浪费. uC/OS 的内存管理机制就是为了尽量减少内存碎片.大致的 ...
- int bool 字符串 列表 字典 集合
1.int和bool 输出i的最大二进制位数inti = 1000 print(i.bit_length()) 2. str int bool list set dict tuple 相互转换 pr ...