在上一篇中总结了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)trim(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)trimToEmpty(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)trimToNull(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, int 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');// 0
StringUtils.indexOf("abca", 'b');// 1
StringUtils.indexOf("abca", 'A');// -1
StringUtils.indexOf(null, "a"); // -1
StringUtils.indexOf("abc", null); // -1
StringUtils.indexOf("", ""); // 0
StringUtils.indexOf("", "a"); // -1 注意这里第二个参数为""时则为0
StringUtils.indexOf("abc", "a"); // 0
StringUtils.indexOf("abc", "b"); // 1
StringUtils.indexOf("abc", "ab"); // 0
StringUtils.indexOf("abc", ""); // 0

    b)不区分大小写:

StringUtils.indexOfIgnoreCase(null, "a"); // -1
StringUtils.indexOfIgnoreCase("abc", null); // -1
StringUtils.indexOfIgnoreCase("", ""); // 0
StringUtils.indexOfIgnoreCase("", "a");// -1
StringUtils.indexOfIgnoreCase("abc", "b));// 1
StringUtils.indexOfIgnoreCase("abc", "B"); // 1

  (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); // 0
StringUtils.indexOf("", "", 2); // 0
StringUtils.indexOf("", "a", 0); // -1 不包括第二个参数为""的情况
StringUtils.indexOf("abac", "a", 1); // 2
StringUtils.indexOf("abcab", "ab", 2); // 3
StringUtils.indexOf("abc", "a", -1); // 0 -1被当作是0
StringUtils.indexOf("abc", "a", 2); // -1

    b)不区分大小写:

StringUtils.indexOfIgnoreCase("", "", 0)); // 0
StringUtils.indexOfIgnoreCase("", "", 0)); // 1 与不忽略大小写的情况不同,下面也是
StringUtils.indexOfIgnoreCase("", "", 0)); //-1
StringUtils.indexOfIgnoreCase("abac", "A", 1)); // 2
StringUtils.indexOfIgnoreCase("abcab", "AB", 2)); // 3
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'));// 5
StringUtils.lastIndexOf("abccba", 'z'));// -1
StringUtils.lastIndexOf(null, "a"));// -1
StringUtils.lastIndexOf("abc", null));// -1
StringUtils.lastIndexOf("", ""));// 0
StringUtils.lastIndexOf("abc", "b"));// 1
StringUtils.lastIndexOf("abc", "ab"));// 0
StringUtils.lastIndexOf("abc", ""));// 3 返回字符串的长度

    b)不区分大小写:

StringUtils.lastIndexOfIgnoreCase(null, "a");// -1
StringUtils.lastIndexOfIgnoreCase("abc", null);// -1
StringUtils.lastIndexOfIgnoreCase("", "");// 0
StringUtils.lastIndexOfIgnoreCase("abc", "B");// 1
StringUtils.lastIndexOfIgnoreCase("abc", "AB");// 0
StringUtils.lastIndexOfIgnoreCase("abc", "");// 3 返回字符串的长度

StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)的更多相关文章

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

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

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

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

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

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

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

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

  5. StringUtils工具类常用方法

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

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

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

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

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

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

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

  9. Spring的StringUtils工具类

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<Spring的StringUtils工具类> org.springframework.util.StringU ...

随机推荐

  1. javaWeb 基础知识

    cookie  深度解析:  http://blog.csdn.net/ghsau/article/details/20395681 cookie 的作用: 利用存在客户端本地的缓存让无状态的服务器也 ...

  2. Python 第十三节 文件操作

    A 1.首先文件读写操作有以下几种模式:   a\a+  w\w+ r\r+   a模式:追加_写入模式,写入指针默认在开头,如果文件存在将在开头追加写入,如果文件不存在将创建文件再写入. a+模式: ...

  3. python的re正则表达

    正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的一个概念.正则表 ...

  4. Mybatis通用Mapper

    极其方便的使用Mybatis单表的增删改查 项目地址:http://git.oschina.net/free/Mapper 优点? 不客气的说,使用这个通用Mapper甚至能改变你对Mybatis单表 ...

  5. poj 3662 Telephone Lines

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7115   Accepted: 2603 D ...

  6. centos 下安装jdk、tomcat 以及tomcat无法从外部访问的解决办法

    centos 下安装jdk.tomcat 以及tomcat无法从外部访问的解决办法 原创 2014年08月28日 10:24:33 标签: selinux enforce cent 2223 昨天在c ...

  7. css变量

    CSS变量: 目前主流浏览器都已支持CSS变量,Edge 浏览器也支持 CSS 变量.用户可以方便地在css中使用自定义变量. <!DOCTYPE html> <html lang= ...

  8. input 密码框调出手机的数字键盘

    对于某些密码,想要在手机上调出数字键盘,同时要隐藏文字.可结合type=tel和 text-security属性达到目的. input{ -webkit-text-security:disc; tex ...

  9. filereader api 类型

    filereader类似XMLHttpRequest,只是它用来从文件系统读取文件,提供了不同的方法去读取文件数据:1.readAsText2.readAsDataURL3.readAsBinaryS ...

  10. spring boot hello and docker

    主要是想试下spring boot运行在docker里的感觉, 小试牛刀   :) 这是原文,参考一下:  https://spring.io/guides/gs/spring-boot-docker ...