一、截取

  StringUtils中常用的截取字符串的方法如下:

  1. substring(String str,int start)
  2. substring(String str,int start, int end)
  3. substringAfter(String str,String separator)
  4. substringAfterLast(String str,String separator)
  5. substringBefore(String str,String separator)
  6. substringBeforeLast(String str,String separator)
  7. substringBetween(String str,String tag)

  需要注意的是,截取字符串时,若被截取的字符串为null或"",则截取之后的返回的字符串也为null和""

  (1)根据指定位置截取字符串,当指定的截取位置为非负数时,则从左往右开始截取,第一位为0,后面依次类推,但当索引值为负数时,则从右往左截取,注意此时右侧第一位为-1

    a)只指定了起始位置,则截取至字符串末尾:

  1. StringUtils.substring(null, 2); // "" null和""截取后都返回null和""
  2. StringUtils.substring(null, 2); // null
  3. StringUtils.substring("china", 0); // china 指定的起始截取位置为0,则从第一位开始截取,也就是不截取
  4. StringUtils.substring("china", 2); // ina 指定的截取位置为2,则从左往右第三位开始截取
  5. StringUtils.substring("china", -2); // na 指定的截取位置为-2,则从右往左第二位开始截取

    b)指定了起始位置和结束位置,则从起始位置开始截取到结束位置(但不包含结束位置):

  1. StringUtils.substring(null, 2, 4); // null null和""截取后都返回null和""
  2. StringUtils.substring("", 2, 4); // ""
  3. StringUtils.substring("china", 0, 0); // ""
  4. StringUtils.substring("china", 2, 4); // in
  5. StringUtils.substring("china", -2, -4); // in
  6. StringUtils.substring("china", 2, -3); // ""
  7. StringUtils.substring("china", 2, -1); // in

    (2)根据指定的分隔符进行截取(不包含该分隔符):
      a)从分隔符第一次出现的位置向后截取:

  1. StringUtils.substringAfter("china", "i"); // na 从第一次出现"i"的位置向后截取,不包含第一次出现的"i"
  2. StringUtils.substringAfter("china", "hi"); // na
  3. StringUtils.substringAfter("chinachina","h")); // inachina
  4. StringUtils.substringAfter("china", "a"); // ""
  5. StringUtils.substringAfter("china", "d"); // "" 分隔符在要截取的字符串中不存在,则返回""
  6. StringUtils.substringAfter("china", "")); // china 分隔符为"",则返回原字符串
  7. Stringtils.substringAfter("china", null); // "" 分隔符为null,则返回""

      b)从分隔符最后一次出现的位置向后截取:

  1. StringUtils.substringAfterLast("china", "i"); // na
  2. StringUtils.substringAfterLast("chinachina", "i"); // na "i"最后出现的位置向后截取

      c)从分隔符第一次出现的位置向前截取:

  1. StringUtils.substringBefore("china", "i"); // ch
  2. StringUtils.substringBefore("chinachina", "i"); // ch 从"i"第一次出现的位置向前截取

      d)从分隔符最后一次出现的位置向前截取:

  1. StringUtils.substringBeforeLast("china", "i");
  2. StringUtils.substringBeforeLast("chinachina", "i"); // chinach

      e)截取指定标记字符串之间的字符序列:

  1. StringUtils.substringBetween(null, "ch") // null
  2. StringUtils.substringBetween("", "") // ""
  3. StringUtils.substringBetween("tagabctag", "") // "" 标记字符串为"",则截取后返回""
  4. StringUtils.substringBetween("", "tag") // null // 注意此处返回的是null
  5. StringUtils.substringBetween("tagabctag", null) // null 标记字符串为null,则截取后返回null
  6. StringUtils.substringBetween("tagabctag", "tag") // "abc"

二、去除空白:

  去除字符串中的空白符是我们在处理字符串时经常遇到的问题,StringUtils中也封装了一些非常好用的方法来帮助我们解决这个问题:

  1. trim(String str)
  2. trimToEmpty(String str)
  3. trimToNull(String str)
  4. strip(String str)
  5. stripToEmpty(String str)
  6. stripToNull(String str)
  7. deleteWhitespace(String str)

  (1)去除字符串首尾的控制符(char ≤ 32)
    a)trim(String str):如果被去除的字符串的为null或"",则返回null和"":

  1. StringUtils.trim(null); // null
  2. StringUtils.trim(""); // ""
  3. StringUtils.trim(" ");// ""
  4. StringUtils.trim("abc"); // abc
  5. StringUtils.trim(" abc "); // abc
  6. StringUtils.trim(" a b c "); // "a b c" 注意此处字符串内部的控制符是不去除的

    b)trimToEmpty(String str):如果被去除的字符串的为null或"",则都返回"":

  1. StringUtils.trimToEmpty(null); // "" 此处返回的是""
  2. StringUtils.trimToEmpty(""); // ""
  3. StringUtils.trimToEmpty(" ");// ""
  4. StringUtils.trimToEmpty("abc"); // abc
  5. StringUtils.trimToEmpty(" abc "); // abc
  6. StringUtils.trimToEmpty(" a b c "); // a b c

    c)trimToNull(String str):如果被去除的字符串的为null或"",则都返回null:

  1. StringUtils.trimToNull(null); // null
  2. StringUtils.trimToNull(""); // null
  3. StringUtils.trimToNull(" ");// null
  4. StringUtils.trimToNull("abc"); // abc
  5. StringUtils.trimToNull(" \t\r\nabc "); // abc
  6. StringUtils.trimToNull(" a b c "); // "a b c"

  (2)去除字符串首尾的空白符(空白符主要包括' ','\t','\r','\n'等等,具体的空白符可以参考Java API中Character类中isWhiteSpace()方法中的描述):
    a)strip(String str):如果被去除的字符串的为null或"",则返回null和"":

  1. StringUtils.strip(null); // null
  2. StringUtils.strip(""); // ""
  3. StringUtils.strip(" ");// ""
  4. StringUtils.strip("abc"); // abc
  5. StringUtils.strip(" \t\r\n abc "); // abc
  6. StringUtils.strip(" a b c "); // a b c

    b)stripToEmpty(String str):如果被去除的字符串的为null或"",则都返回"":

  1. StringUtils.stripToEmpty(null); // null
  2. StringUtils.stripToEmpty(""); // nulld
  3. StringUtils.stripToEmpty(" ");// null
  4. StringUtils.stripToEmpty("abc"); // abc
  5. StringUtils.stripToEmpty(" \t\r\n abc "); // abc
  6. StringUtils.stripToEmpty(" a b c "); // "a b c"

    c)stripToNull(String str):如果被去除的字符串的为null或"",则都返回null:

  1. StringUtils.stripToNull(null); // null
  2. StringUtils.stripToNull(""); // nulld
  3. StringUtils.stripToNull(" ");// null
  4. StringUtils.stripToNull("abc"); // abc
  5. StringUtils.stripToNull(" \t\r\n abc "); // abc
  6. StringUtils.stripToNull(" a b c "); // "a b c"

  (2)去除字符串中所有的空白符

  1. StringUtils.deleteWhitespace(null); // null
  2. StringUtils.deleteWhitespace(""); // ""
  3. StringUtils.deleteWhitespace("abc"); // "abc"
  4. StringUtils.deleteWhitespace(" ab c "); // "abc"

三、包含:

  StringUtils中判断是否包含的方法主要有:

  1. contains(CharSequence seq, char searchChar)
  2. contains(CharSequence seq, CharSequence searchSeq)
  3. containsIgnoreCase(CharSequence str, CharSequence searchStr)
  4. containsAny(CharSequence cs, char... searchChars)
  5. containsAny(CharSequence cs, CharSequence searchChars)
  6. containsOnly(CharSequence cs,char... valid)
  7. containsOnly(CharSequence cs, String validChars)
  8. containsNone(CharSequence cs,char... searchChars)
  9. containsNone(CharSequence cs, String invalidChars)
  10. startsWith(CharSequence str,CharSequence prefix)
  11. startsWithIgnoreCase(CharSequence str,CharSequence prefix)
  12. startsWithAny(CharSequence string,CharSequence searchStrings)

  (1)判断字符串中是否包含指定的字符或字符序列:
    a)区分大小写:

  1. StringUtils.contains(null, 'a'); // false
  2. StringUtils.contains("china", null); // false
  3. StringUtils.contains("", 'a'); // false
  4. StringUtils.contains("china", 'a');// true
  5. StringUtils.contains("china", 'z');//false
  6. StringUtils.contains(null, "a"); // false
  7. StringUtils.contains("china", null); // false
  8. StringUtils.contains("", ""); // true
  9. StringUtils.contains("abc", "");// true
  10. StringUtils.contains("china", "na");// true
  11. StringUtils.contains("abc", "z"); // false

    b)不区分大小写:

  1. StringUtils.containsIgnoreCase("china", 'a');// true
  2. StringUtils.containsIgnoreCase("china", 'A');// true
  3. StringUtils.containsIgnoreCase("china", 'Z');//false
  4. StringUtils.containsIgnoreCase(null, "A"); // false
  5. StringUtils.containsIgnoreCase("china", null); // false
  6. StringUtils.containsIgnoreCase("", ""); // true
  7. StringUtils.containsIgnoreCase("abc", "");// true
  8. StringUtils.containsIgnoreCase("china", "na");// true
  9. StringUtils.containsIgnoreCase("china", "Na");// true
  10. StringUtils.containsIgnoreCase("abc", "Z"); // false

  (2)判断字符串中是否包含指定字符集合中或指定字符串中任一字符,区分大小写:

  1. StringUtils.containsAny(null, 'a', 'b');// false
  2. StringUtils.containsAny("", 'a', 'b');// false
  3. StringUtils.containsAny("abc", 'a', 'z');// true
  4. StringUtils.containsAny("abc", 'x', 'y');// false
  5. StringUtils.containsAny("abc", 'A', 'z');// false
  6. StringUtils.containsAny(null, "a");// false
  7. StringUtils.containsAny("", "a");// false
  8. StringUtils.containsAny("abc", "ab");// true
  9. StringUtils.containsAny("abc", "ax");// true
  10. StringUtils.containsAny("abc", "xy");// false
  11. StringUtils.containsAny("abc", "Ax");// false

  (3)判断字符串中是否不包含指定的字符或指定的字符串中的字符,区分大小写:

  1. StringUtils.containsNone(null, 'a'); // true
  2. StringUtils.containsNone("", 'a'); // true 注意这里,空串总是返回true
  3. StringUtils.containsNone("china", ' '); // true 注意包含空白符为true
  4. StringUtils.containsNone("china", '\t'); // true
  5. StringUtils.containsNone("china", '\r'); // true
  6. StringUtils.containsNone("china", 'x', 'y', 'z'); // true
  7. StringUtils.containsNone("china", 'c', 'y', 'z'); // false
  8. StringUtils.containsNone("china", 'C', 'y', 'z'); // true
  9. StringUtils.containsNone(null, "a"); // true
  10. StringUtils.containsNone("", "a"); // true
  11. StringUtils.containsNone("china", ""); // true
  12. StringUtils.containsNone("china", "xyz"); // true
  13. StringUtils.containsNone("china", "cyz"); // false
  14. StringUtils.containsNone("china", "Cyz"); // true

  (4)判断字符串中的字符是否都是出自所指定的字符数组或字符串,区分大小写:

  1. StringUtils.containsOnly(null, 'a');// false
  2. StringUtils.containsOnly("", "a");// true
  3. StringUtils.containsOnly("ab", ' ');// false
  4. StringUtils.containsOnly("abab", 'a', 'b', 'c');// true
  5. StringUtils.containsOnly("abcd", 'a', 'b', 'c');// false
  6. StringUtils.containsOnly("Abab", 'a', 'b', 'c');// false
  7. StringUtils.containsOnly(null, "a");// false
  8. StringUtils.containsOnly("", "a"); // true
  9. StringUtils.containsOnly("abab", "abc));// true
  10. StringUtils.containsOnly("abcd", "abc"); // false
  11. StringUtils.containsOnly("Abab", "abc");// false

  (5)判断字符串是否以指定的字符序列开头:
    a)区分大小写:

  1. StringUtils.startsWith(null, null); // true
  2. StringUtils.startsWith(null, "abc"); // false
  3. StringUtils.startsWith("abcdef", null); // false
  4. StringUtils.startsWith("abcdef", "abc"); // true
  5. StringUtils.startsWith("ABCDEF", "abc"); // false

    b)不区分大小写:

  1. StringUtils.startsWithIgnoreCase(null, null);// true
  2. StringUtils.startsWithIgnoreCase(null, "abc");// false
  3. StringUtils.startsWithIgnoreCase("abcdef", null);// false
  4. StringUtils.startsWithIgnoreCase("abcdef", "abc");// true
  5. StringUtils.startsWithIgnoreCase("ABCDEF", "abc");// true

  (6)判断字符串是否以指定的字符序列数组中任意一个开头,区分大小写:

  1. StringUtils.startsWithAny(null, null);// false
  2. StringUtils.startsWithAny(null, new String[] { "abc" });// false
  3. StringUtils.startsWithAny("abcxyz", null);// false
  4. StringUtils.startsWithAny("abcxyz", new String[] { "" });// true
  5. StringUtils.startsWithAny("abcxyz", new String[] { "abc" });// true
  6. StringUtils.startsWithAny("abcxyz", new String[] { null, "xyz", "abc" });// true
  7. StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX");// false
  8. StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc");// false

四、查询索引:

  StringUtils中获取字符或字符序列在字符串中出现的索引下标的方法主要有:

  1. indexOf(CharSequence seq, int searchChar)
  2. indexOf(CharSequence seq,CharSequence searchSeq)
  3. indexOfIgnoreCase
  4. indexOf(CharSequence seq,CharSequence searchSeq,int startPos)
  5. lastIndexOf(CharSequence seq,int searchChar)
  6. lastIndexOfIgnoreCase(CharSequence str,CharSequence searchStr)

  (1)获取指定字符或字符序列在字符串中第一次出现的索引,若字符串中不包含该字符或字符序列,则返回-1,若字符串或字符序列为""或null,也返回-1((但字符串和字符序列都为""的情况下,则返回0)):
    a)区分大小写:

  1. StringUtils.indexOf(null, 'a');// -1
  2. StringUtils.indexOf("", 'a');// -1
  3. StringUtils.indexOf("abca", 'a');//
  4. StringUtils.indexOf("abca", 'b');//
  5. StringUtils.indexOf("abca", 'A');// -1
  6. StringUtils.indexOf(null, "a"); // -1
  7. StringUtils.indexOf("abc", null); // -1
  8. StringUtils.indexOf("", ""); //
  9. StringUtils.indexOf("", "a"); // -1 注意这里第二个参数为""时则为0
  10. StringUtils.indexOf("abc", "a"); //
  11. StringUtils.indexOf("abc", "b"); //
  12. StringUtils.indexOf("abc", "ab"); //
  13. StringUtils.indexOf("abc", ""); //

    b)不区分大小写:

  1. StringUtils.indexOfIgnoreCase(null, "a"); // -1
  2. StringUtils.indexOfIgnoreCase("abc", null); // -1
  3. StringUtils.indexOfIgnoreCase("", ""); //
  4. StringUtils.indexOfIgnoreCase("", "a");// -1
  5. StringUtils.indexOfIgnoreCase("abc", "b));// 1
  6. StringUtils.indexOfIgnoreCase("abc", "B"); //

  (1)获取字符序列在字符串中指定位置之后第一次出现的索引,若字符串中指定位置之后不包含该字符序列,则返回-1,若字符串或字符序列为""或null,也返回-1(但字符串和字符序列都为""的情况下,结果就有点怪异,有时返回0,有时返回1,有时返回-1,根据指定的起始位置会有变化)
    a)区分大小写:

  1. StringUtils.indexOf(null, "a", 2); // -1
  2. StringUtils.indexOf("abc", null, 2); // -1
  3. StringUtils.indexOf("", "", 0); // 0 注意此处和下一行都返回0,对比忽略大小写的情形,就有点不一样
  4. StringUtils.indexOf("", "", 1); //
  5. StringUtils.indexOf("", "", 2); // 0
  6. StringUtils.indexOf("", "a", 0); // -1 不包括第二个参数为""的情况
  7. StringUtils.indexOf("abac", "a", 1); //
  8. StringUtils.indexOf("abcab", "ab", 2); //
  9. StringUtils.indexOf("abc", "a", -1); // 0 -1被当作是0
  10. StringUtils.indexOf("abc", "a", 2); // -1

    b)不区分大小写:

  1. StringUtils.indexOfIgnoreCase("", "", 0)); //
  2. StringUtils.indexOfIgnoreCase("", "", 0)); // 1 与不忽略大小写的情况不同,下面也是
  3. StringUtils.indexOfIgnoreCase("", "", 0)); //-1
  4. StringUtils.indexOfIgnoreCase("abac", "A", 1)); //
  5. StringUtils.indexOfIgnoreCase("abcab", "AB", 2)); //
  6. StringUtils.indexOfIgnoreCase("abc", "B", -1)); // 1 -1被当作是0

  (2)获取指定字符或字符序列在字符串中最后一次出现的索引,若字符串中不包含该字符序列,则返回-1,若字符串或字符序列为""或null,也返回-1(但字符串和字符序列都为""的情况下,返回0)
    a)区分大小写:

  1. StringUtils.lastIndexOf(null, 'a'));// -1
  2. StringUtils.lastIndexOf("", 'a'));// -1
  3. StringUtils.lastIndexOf("abccba", 'a'));//
  4. StringUtils.lastIndexOf("abccba", 'z'));// -1
  5. StringUtils.lastIndexOf(null, "a"));// -1
  6. StringUtils.lastIndexOf("abc", null));// -1
  7. StringUtils.lastIndexOf("", ""));//
  8. StringUtils.lastIndexOf("abc", "b"));//
  9. StringUtils.lastIndexOf("abc", "ab"));//
  10. StringUtils.lastIndexOf("abc", ""));// 3 返回字符串的长度

    b)不区分大小写:

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

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

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

      在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取   StringUtils ...

  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. java生成excel,word文件

    第一部分: 在网站开发中,用户可能需要打印word或者excel表,这种需求是非常多的. java语言生成excel表格和python的方式有点像,使用Apache POI的组件,一通全通.开发过程通 ...

  2. 最难解的耦合 — James

    最近好久没写博客,因为换工作原因,从传统企业转行到互联网行业,这次换工作成本很大! 新公司,纯互联网广告公司,BI驱动,出来几日感觉成长空间很大,下面来些干货. 什么是程序.软件 程序 = 数据结构 ...

  3. java基础学习总结——流

    一.JAVA流式输入/输出原理

  4. vue里的渲染以及computed的好处

    如果vue里的某个methods函数执行,导致页面重新渲染,那么所有页面渲染相关的methods函数会重新执行以及时的渲染页面 但是大量函数的重新没有必要的执行会导致性能的下降, 此时如果把没有必要再 ...

  5. AUC计算 - 手把手步进操作

    2017-07-10 14:38:24 理论参考: 评估分类器性能的度量,像混淆矩阵.ROC.AUC等 http://www.cnblogs.com/suanec/p/5941630.html ROC ...

  6. angularjs使用BUG收集和解决办法

    此文章涉及到时1.X的版本.请注意! 1.关于checkbox和bootstrap不能选中BUG 在使用angularjs的时候,有个比较明显的bug ng-disabled无效的情况 这里是一种情况 ...

  7. hive on spark配置

    1.安装java.maven.scala.hadoop.mysql.hive 略 2.编译spark ./make-distribution.sh --name "hadoop2-witho ...

  8. 实时分析(在线查询),firehose---clickhouse

    firehose---clickhouse 在Hive中适不适合像传统数据仓库一样利用维度建模hive新功能 Cube, Rollup介绍https://blog.csdn.net/moon_yang ...

  9. Linux的is not in the sudoers file 解决

    https://blog.csdn.net/hxpjava1/article/details/79566822

  10. OC OD介绍

    参考:http://www.elecfans.com/baike/bandaoti/jichuzhishi/20100304178298.html OC门,又称集电极开路门,Open Collecto ...