StringUtils工具类常用方法汇总(截取、去除空白、包含、查询索引)
一、截取
StringUtils中常用的截取字符串的方法如下:
- substring(String str,int start)
- substring(String str,int start, int end)
- substringAfter(String str,String separator)
- substringAfterLast(String str,String separator)
- substringBefore(String str,String separator)
- substringBeforeLast(String str,String separator)
- substringBetween(String str,String tag)
需要注意的是,截取字符串时,若被截取的字符串为null或"",则截取之后的返回的字符串也为null和""。
(1)根据指定位置截取字符串,当指定的截取位置为非负数时,则从左往右开始截取,第一位为0,后面依次类推,但当索引值为负数时,则从右往左截取,注意此时右侧第一位为-1:
a)只指定了起始位置,则截取至字符串末尾:
- StringUtils.substring(null, 2); // "" null和""截取后都返回null和""
- StringUtils.substring(null, 2); // null
- StringUtils.substring("china", 0); // china 指定的起始截取位置为0,则从第一位开始截取,也就是不截取
- StringUtils.substring("china", 2); // ina 指定的截取位置为2,则从左往右第三位开始截取
- StringUtils.substring("china", -2); // na 指定的截取位置为-2,则从右往左第二位开始截取
b)指定了起始位置和结束位置,则从起始位置开始截取到结束位置(但不包含结束位置):
- StringUtils.substring(null, 2, 4); // null null和""截取后都返回null和""
- StringUtils.substring("", 2, 4); // ""
- StringUtils.substring("china", 0, 0); // ""
- StringUtils.substring("china", 2, 4); // in
- StringUtils.substring("china", -2, -4); // in
- StringUtils.substring("china", 2, -3); // ""
- StringUtils.substring("china", 2, -1); // in
(2)根据指定的分隔符进行截取(不包含该分隔符):
a)从分隔符第一次出现的位置向后截取:
- StringUtils.substringAfter("china", "i"); // na 从第一次出现"i"的位置向后截取,不包含第一次出现的"i"
- StringUtils.substringAfter("china", "hi"); // na
- StringUtils.substringAfter("chinachina","h")); // inachina
- StringUtils.substringAfter("china", "a"); // ""
- StringUtils.substringAfter("china", "d"); // "" 分隔符在要截取的字符串中不存在,则返回""
- StringUtils.substringAfter("china", "")); // china 分隔符为"",则返回原字符串
- Stringtils.substringAfter("china", null); // "" 分隔符为null,则返回""
b)从分隔符最后一次出现的位置向后截取:
- StringUtils.substringAfterLast("china", "i"); // na
- StringUtils.substringAfterLast("chinachina", "i"); // na "i"最后出现的位置向后截取
c)从分隔符第一次出现的位置向前截取:
- StringUtils.substringBefore("china", "i"); // ch
- StringUtils.substringBefore("chinachina", "i"); // ch 从"i"第一次出现的位置向前截取
d)从分隔符最后一次出现的位置向前截取:
- StringUtils.substringBeforeLast("china", "i");
- StringUtils.substringBeforeLast("chinachina", "i"); // chinach
e)截取指定标记字符串之间的字符序列:
- StringUtils.substringBetween(null, "ch") // null
- StringUtils.substringBetween("", "") // ""
- StringUtils.substringBetween("tagabctag", "") // "" 标记字符串为"",则截取后返回""
- StringUtils.substringBetween("", "tag") // null // 注意此处返回的是null
- StringUtils.substringBetween("tagabctag", null) // null 标记字符串为null,则截取后返回null
- StringUtils.substringBetween("tagabctag", "tag") // "abc"
二、去除空白:
去除字符串中的空白符是我们在处理字符串时经常遇到的问题,StringUtils中也封装了一些非常好用的方法来帮助我们解决这个问题:
- trim(String str)
- trimToEmpty(String str)
- trimToNull(String str)
- strip(String str)
- stripToEmpty(String str)
- stripToNull(String str)
- deleteWhitespace(String str)
(1)去除字符串首尾的控制符(char ≤ 32)
a)trim(String str):如果被去除的字符串的为null或"",则返回null和"":
- StringUtils.trim(null); // null
- StringUtils.trim(""); // ""
- StringUtils.trim(" ");// ""
- StringUtils.trim("abc"); // abc
- StringUtils.trim(" abc "); // abc
- StringUtils.trim(" a b c "); // "a b c" 注意此处字符串内部的控制符是不去除的
b)trimToEmpty(String str):如果被去除的字符串的为null或"",则都返回"":
- StringUtils.trimToEmpty(null); // "" 此处返回的是""
- StringUtils.trimToEmpty(""); // ""
- StringUtils.trimToEmpty(" ");// ""
- StringUtils.trimToEmpty("abc"); // abc
- StringUtils.trimToEmpty(" abc "); // abc
- StringUtils.trimToEmpty(" a b c "); // a b c
c)trimToNull(String str):如果被去除的字符串的为null或"",则都返回null:
- StringUtils.trimToNull(null); // null
- StringUtils.trimToNull(""); // null
- StringUtils.trimToNull(" ");// null
- StringUtils.trimToNull("abc"); // abc
- StringUtils.trimToNull(" \t\r\nabc "); // abc
- StringUtils.trimToNull(" a b c "); // "a b c"
(2)去除字符串首尾的空白符(空白符主要包括' ','\t','\r','\n'等等,具体的空白符可以参考Java API中Character类中isWhiteSpace()方法中的描述):
a)strip(String str):如果被去除的字符串的为null或"",则返回null和"":
- StringUtils.strip(null); // null
- StringUtils.strip(""); // ""
- StringUtils.strip(" ");// ""
- StringUtils.strip("abc"); // abc
- StringUtils.strip(" \t\r\n abc "); // abc
- StringUtils.strip(" a b c "); // a b c
b)stripToEmpty(String str):如果被去除的字符串的为null或"",则都返回"":
- StringUtils.stripToEmpty(null); // null
- StringUtils.stripToEmpty(""); // nulld
- StringUtils.stripToEmpty(" ");// null
- StringUtils.stripToEmpty("abc"); // abc
- StringUtils.stripToEmpty(" \t\r\n abc "); // abc
- StringUtils.stripToEmpty(" a b c "); // "a b c"
c)stripToNull(String str):如果被去除的字符串的为null或"",则都返回null:
- StringUtils.stripToNull(null); // null
- StringUtils.stripToNull(""); // nulld
- StringUtils.stripToNull(" ");// null
- StringUtils.stripToNull("abc"); // abc
- StringUtils.stripToNull(" \t\r\n abc "); // abc
- StringUtils.stripToNull(" a b c "); // "a b c"
(2)去除字符串中所有的空白符:
- StringUtils.deleteWhitespace(null); // null
- StringUtils.deleteWhitespace(""); // ""
- StringUtils.deleteWhitespace("abc"); // "abc"
- StringUtils.deleteWhitespace(" ab c "); // "abc"
三、包含:
StringUtils中判断是否包含的方法主要有:
- contains(CharSequence seq, char searchChar)
- contains(CharSequence seq, CharSequence searchSeq)
- containsIgnoreCase(CharSequence str, CharSequence searchStr)
- containsAny(CharSequence cs, char... searchChars)
- containsAny(CharSequence cs, CharSequence searchChars)
- containsOnly(CharSequence cs,char... valid)
- containsOnly(CharSequence cs, String validChars)
- containsNone(CharSequence cs,char... searchChars)
- containsNone(CharSequence cs, String invalidChars)
- startsWith(CharSequence str,CharSequence prefix)
- startsWithIgnoreCase(CharSequence str,CharSequence prefix)
- startsWithAny(CharSequence string,CharSequence… searchStrings)
(1)判断字符串中是否包含指定的字符或字符序列:
a)区分大小写:
- StringUtils.contains(null, 'a'); // false
- StringUtils.contains("china", null); // false
- StringUtils.contains("", 'a'); // false
- StringUtils.contains("china", 'a');// true
- StringUtils.contains("china", 'z');//false
- StringUtils.contains(null, "a"); // false
- StringUtils.contains("china", null); // false
- StringUtils.contains("", ""); // true
- StringUtils.contains("abc", "");// true
- StringUtils.contains("china", "na");// true
- StringUtils.contains("abc", "z"); // false
b)不区分大小写:
- StringUtils.containsIgnoreCase("china", 'a');// true
- StringUtils.containsIgnoreCase("china", 'A');// true
- StringUtils.containsIgnoreCase("china", 'Z');//false
- StringUtils.containsIgnoreCase(null, "A"); // false
- StringUtils.containsIgnoreCase("china", null); // false
- StringUtils.containsIgnoreCase("", ""); // true
- StringUtils.containsIgnoreCase("abc", "");// true
- StringUtils.containsIgnoreCase("china", "na");// true
- StringUtils.containsIgnoreCase("china", "Na");// true
- StringUtils.containsIgnoreCase("abc", "Z"); // false
(2)判断字符串中是否包含指定字符集合中或指定字符串中任一字符,区分大小写:
- StringUtils.containsAny(null, 'a', 'b');// false
- StringUtils.containsAny("", 'a', 'b');// false
- StringUtils.containsAny("abc", 'a', 'z');// true
- StringUtils.containsAny("abc", 'x', 'y');// false
- StringUtils.containsAny("abc", 'A', 'z');// false
- StringUtils.containsAny(null, "a");// false
- StringUtils.containsAny("", "a");// false
- StringUtils.containsAny("abc", "ab");// true
- StringUtils.containsAny("abc", "ax");// true
- StringUtils.containsAny("abc", "xy");// false
- StringUtils.containsAny("abc", "Ax");// false
(3)判断字符串中是否不包含指定的字符或指定的字符串中的字符,区分大小写:
- StringUtils.containsNone(null, 'a'); // true
- StringUtils.containsNone("", 'a'); // true 注意这里,空串总是返回true
- StringUtils.containsNone("china", ' '); // true 注意包含空白符为true
- StringUtils.containsNone("china", '\t'); // true
- StringUtils.containsNone("china", '\r'); // true
- StringUtils.containsNone("china", 'x', 'y', 'z'); // true
- StringUtils.containsNone("china", 'c', 'y', 'z'); // false
- StringUtils.containsNone("china", 'C', 'y', 'z'); // true
- StringUtils.containsNone(null, "a"); // true
- StringUtils.containsNone("", "a"); // true
- StringUtils.containsNone("china", ""); // true
- StringUtils.containsNone("china", "xyz"); // true
- StringUtils.containsNone("china", "cyz"); // false
- StringUtils.containsNone("china", "Cyz"); // true
(4)判断字符串中的字符是否都是出自所指定的字符数组或字符串,区分大小写:
- StringUtils.containsOnly(null, 'a');// false
- StringUtils.containsOnly("", "a");// true
- StringUtils.containsOnly("ab", ' ');// false
- StringUtils.containsOnly("abab", 'a', 'b', 'c');// true
- StringUtils.containsOnly("abcd", 'a', 'b', 'c');// false
- StringUtils.containsOnly("Abab", 'a', 'b', 'c');// false
- StringUtils.containsOnly(null, "a");// false
- StringUtils.containsOnly("", "a"); // true
- StringUtils.containsOnly("abab", "abc));// true
- StringUtils.containsOnly("abcd", "abc"); // false
- StringUtils.containsOnly("Abab", "abc");// false
(5)判断字符串是否以指定的字符序列开头:
a)区分大小写:
- StringUtils.startsWith(null, null); // true
- StringUtils.startsWith(null, "abc"); // false
- StringUtils.startsWith("abcdef", null); // false
- StringUtils.startsWith("abcdef", "abc"); // true
- StringUtils.startsWith("ABCDEF", "abc"); // false
b)不区分大小写:
- StringUtils.startsWithIgnoreCase(null, null);// true
- StringUtils.startsWithIgnoreCase(null, "abc");// false
- StringUtils.startsWithIgnoreCase("abcdef", null);// false
- StringUtils.startsWithIgnoreCase("abcdef", "abc");// true
- StringUtils.startsWithIgnoreCase("ABCDEF", "abc");// true
(6)判断字符串是否以指定的字符序列数组中任意一个开头,区分大小写:
- StringUtils.startsWithAny(null, null);// false
- StringUtils.startsWithAny(null, new String[] { "abc" });// false
- StringUtils.startsWithAny("abcxyz", null);// false
- StringUtils.startsWithAny("abcxyz", new String[] { "" });// true
- StringUtils.startsWithAny("abcxyz", new String[] { "abc" });// true
- StringUtils.startsWithAny("abcxyz", new String[] { null, "xyz", "abc" });// true
- StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX");// false
- StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc");// false
四、查询索引:
StringUtils中获取字符或字符序列在字符串中出现的索引下标的方法主要有:
- indexOf(CharSequence seq, int searchChar)
- indexOf(CharSequence seq,CharSequence searchSeq)
- indexOfIgnoreCase
- indexOf(CharSequence seq,CharSequence searchSeq,int startPos)
- lastIndexOf(CharSequence seq,int searchChar)
- lastIndexOfIgnoreCase(CharSequence str,CharSequence searchStr)
(1)获取指定字符或字符序列在字符串中第一次出现的索引,若字符串中不包含该字符或字符序列,则返回-1,若字符串或字符序列为""或null,也返回-1((但字符串和字符序列都为""的情况下,则返回0)):
a)区分大小写:
- StringUtils.indexOf(null, 'a');// -1
- StringUtils.indexOf("", 'a');// -1
- StringUtils.indexOf("abca", 'a');//
- StringUtils.indexOf("abca", 'b');//
- StringUtils.indexOf("abca", 'A');// -1
- StringUtils.indexOf(null, "a"); // -1
- StringUtils.indexOf("abc", null); // -1
- StringUtils.indexOf("", ""); //
- StringUtils.indexOf("", "a"); // -1 注意这里第二个参数为""时则为0
- StringUtils.indexOf("abc", "a"); //
- StringUtils.indexOf("abc", "b"); //
- StringUtils.indexOf("abc", "ab"); //
- StringUtils.indexOf("abc", ""); //
b)不区分大小写:
- StringUtils.indexOfIgnoreCase(null, "a"); // -1
- StringUtils.indexOfIgnoreCase("abc", null); // -1
- StringUtils.indexOfIgnoreCase("", ""); //
- StringUtils.indexOfIgnoreCase("", "a");// -1
- StringUtils.indexOfIgnoreCase("abc", "b));// 1
- StringUtils.indexOfIgnoreCase("abc", "B"); //
(1)获取字符序列在字符串中指定位置之后第一次出现的索引,若字符串中指定位置之后不包含该字符序列,则返回-1,若字符串或字符序列为""或null,也返回-1(但字符串和字符序列都为""的情况下,结果就有点怪异,有时返回0,有时返回1,有时返回-1,根据指定的起始位置会有变化):
a)区分大小写:
- StringUtils.indexOf(null, "a", 2); // -1
- StringUtils.indexOf("abc", null, 2); // -1
- StringUtils.indexOf("", "", 0); // 0 注意此处和下一行都返回0,对比忽略大小写的情形,就有点不一样
- StringUtils.indexOf("", "", 1); //
- StringUtils.indexOf("", "", 2); // 0
- StringUtils.indexOf("", "a", 0); // -1 不包括第二个参数为""的情况
- StringUtils.indexOf("abac", "a", 1); //
- StringUtils.indexOf("abcab", "ab", 2); //
- StringUtils.indexOf("abc", "a", -1); // 0 -1被当作是0
- StringUtils.indexOf("abc", "a", 2); // -1
b)不区分大小写:
- StringUtils.indexOfIgnoreCase("", "", 0)); //
- StringUtils.indexOfIgnoreCase("", "", 0)); // 1 与不忽略大小写的情况不同,下面也是
- StringUtils.indexOfIgnoreCase("", "", 0)); //-1
- StringUtils.indexOfIgnoreCase("abac", "A", 1)); //
- StringUtils.indexOfIgnoreCase("abcab", "AB", 2)); //
- StringUtils.indexOfIgnoreCase("abc", "B", -1)); // 1 -1被当作是0
(2)获取指定字符或字符序列在字符串中最后一次出现的索引,若字符串中不包含该字符序列,则返回-1,若字符串或字符序列为""或null,也返回-1(但字符串和字符序列都为""的情况下,返回0):
a)区分大小写:
- StringUtils.lastIndexOf(null, 'a'));// -1
- StringUtils.lastIndexOf("", 'a'));// -1
- StringUtils.lastIndexOf("abccba", 'a'));//
- StringUtils.lastIndexOf("abccba", 'z'));// -1
- StringUtils.lastIndexOf(null, "a"));// -1
- StringUtils.lastIndexOf("abc", null));// -1
- StringUtils.lastIndexOf("", ""));//
- StringUtils.lastIndexOf("abc", "b"));//
- StringUtils.lastIndexOf("abc", "ab"));//
- StringUtils.lastIndexOf("abc", ""));// 3 返回字符串的长度
b)不区分大小写:
- StringUtils.lastIndexOfIgnoreCase(null, "a");// -1
- StringUtils.lastIndexOfIgnoreCase("abc", null);// -1
- StringUtils.lastIndexOfIgnoreCase("", "");//
- StringUtils.lastIndexOfIgnoreCase("abc", "B");//
- StringUtils.lastIndexOfIgnoreCase("abc", "AB");//
- StringUtils.lastIndexOfIgnoreCase("abc", "");// 3 返回字符串的长度
StringUtils工具类常用方法汇总(截取、去除空白、包含、查询索引)的更多相关文章
- StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取 StringUtils ...
- StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方 ...
- StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- StringUtils工具类常用方法汇总:判空、转换、移除、替换、反转。
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- StringUtils工具类常用方法
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- 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 ...
- Spring的StringUtils工具类
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<Spring的StringUtils工具类> org.springframework.util.StringU ...
随机推荐
- java生成excel,word文件
第一部分: 在网站开发中,用户可能需要打印word或者excel表,这种需求是非常多的. java语言生成excel表格和python的方式有点像,使用Apache POI的组件,一通全通.开发过程通 ...
- 最难解的耦合 — James
最近好久没写博客,因为换工作原因,从传统企业转行到互联网行业,这次换工作成本很大! 新公司,纯互联网广告公司,BI驱动,出来几日感觉成长空间很大,下面来些干货. 什么是程序.软件 程序 = 数据结构 ...
- java基础学习总结——流
一.JAVA流式输入/输出原理
- vue里的渲染以及computed的好处
如果vue里的某个methods函数执行,导致页面重新渲染,那么所有页面渲染相关的methods函数会重新执行以及时的渲染页面 但是大量函数的重新没有必要的执行会导致性能的下降, 此时如果把没有必要再 ...
- AUC计算 - 手把手步进操作
2017-07-10 14:38:24 理论参考: 评估分类器性能的度量,像混淆矩阵.ROC.AUC等 http://www.cnblogs.com/suanec/p/5941630.html ROC ...
- angularjs使用BUG收集和解决办法
此文章涉及到时1.X的版本.请注意! 1.关于checkbox和bootstrap不能选中BUG 在使用angularjs的时候,有个比较明显的bug ng-disabled无效的情况 这里是一种情况 ...
- hive on spark配置
1.安装java.maven.scala.hadoop.mysql.hive 略 2.编译spark ./make-distribution.sh --name "hadoop2-witho ...
- 实时分析(在线查询),firehose---clickhouse
firehose---clickhouse 在Hive中适不适合像传统数据仓库一样利用维度建模hive新功能 Cube, Rollup介绍https://blog.csdn.net/moon_yang ...
- Linux的is not in the sudoers file 解决
https://blog.csdn.net/hxpjava1/article/details/79566822
- OC OD介绍
参考:http://www.elecfans.com/baike/bandaoti/jichuzhishi/20100304178298.html OC门,又称集电极开路门,Open Collecto ...