• Java中split方法简介


    1.split()方法介绍

    语法:

    public String[] split(String regex)

    public String[] split(String regex, int limit)

    功能:

    通过指定的分隔符将字符串分割成若干个子字符串,子字符串数组通过返回值返回。

    参数:

    regex : 应用于字符串的正则表达式。

    limit :数组中字符串的数量限制。如果它为零,它将返回所有匹配正则表达式的字符串。

    返回值:

    字符串数组

    例外: PatternSyntaxException 如果正则表达式的模式无效

    2.代码案例

    2.1 单个分隔符

    字符串中仅包含单个分隔符,通过单个分隔符将字符串分割成若干个子字符串。

    1. public class Test {
    2. public static void main(String[] args) {
    3. String str = "2018,text,今天";
    4. // 单个分隔符用引号括起来即可
    5. String[] data = str.split(",");
    6. for (int i = 0; i < data.length; i++) {
    7. System.out.println(data[i]);
    8. }
    9. }
    10. }

    运行结果:

    2018

    text

    今天

    2.2 如果分隔符本身就是"|",那么就需要使用转义字符"\"让其产生效果。

    字符串中包含"|"分隔符,需要通过"|"分隔符将字符串分割成若干个子字符串。

    1. public class Test {
    2. public static void main(String[] args) {
    3. String str = "a|bc|8";
    4. // java中\\表示一个普通\,\|特殊字符表示字符本身
    5. String[] data = str.split("\\|");
    6. for (int i = 0; i < data.length; i++) {
    7. System.out.println(data[i]);
    8. }
    9. }
    10. }

    运行结果:

    a

    bc

    8

    2.3 字符串拆分为字符数组

    将字符串分隔成单个字符数组。

    方式1.用"|"

    方式2.用"(?!^)"

    如下所示:

    1. public class Test {
    2. public static void main(String[] args) {
    3. String str = "a|bc|8";
    4. // 方式1.java中\\表示一个普通\,\|特殊字符表示字符本身
    5. String[] data = str.split("|");
    6. // 方式2.用"(?!^)"也能表示字符本身
    7. // String[] data = str.split("(?!^)");
    8. for (int i = 0; i < data.length; i++) {
    9. System.out.println(data[i]);
    10. }
    11. }
    12. }

    运行结果:

    a

    |

    b

    c

    |

    8

    2.4 指定字符串数量限制

    字符串分割成若干个子字符串,限制子字符串的个数。

    如果 limit > 0,(从左到右)最多分割 n - 1 次,数组的长度将不会大于 n,结尾的空字符串不会丢弃。

    如果 limit < 0,匹配到多少次就分割多少次,而且数组可以是任何长度。结尾的空字符串不会丢弃。

    如果 limit = 0,匹配到多少次就分割多少次,数组可以是任何长度,并且结尾空字符串将被丢弃。

    将limit 设置为 0:这将排除尾随的空字符串。

    将limit 指定为 1,因此 split 方法仅返回1个子字符串。

    将limit 指定为 2,因此 split 方法仅返回2个子字符串。

    当limit 为负数时,它会在输出中包含尾随的空字符串。

    1. public class Test {
    2. public static void main(String[] args) {
    3. String s1 = "hello/hi/bye///";
    4. for (String w : s1.split("/", 0)) { // 将limit 设置为 0:这将排除尾随的空字符串。
    5. System.out.println("returning words1:" + w);
    6. }
    7. System.out.println("------------------");
    8. for (String w : s1.split("/", 1)) { // 将limit 指定为 1,因此 split 方法仅返回1个子字符串。
    9. System.out.println("returning words2:" + w);
    10. }
    11. System.out.println("------------------");
    12. for (String w : s1.split("/", 2)) { // 将limit 指定为 2,因此 split 方法仅返回2个子字符串。
    13. System.out.println("returning words3:" + w);
    14. }
    15. System.out.println("------------------");
    16. int i = 1;
    17. for (String w : s1.split("/", -1)) { // 当limit 为负数时,它会在输出中包含尾随的空字符串。
    18. System.out.print(i++ + ". ");
    19. System.out.println(w);
    20. }
    21. }
    22. }

    运行结果:

    returning words1:hello

    returning words1:hi

    returning words1:bye

    ------------------

    returning words2:hello/hi/bye///

    ------------------

    returning words3:hello

    returning words3:hi/bye///

    ------------------

    1. hello

    2. hi

    3. bye

    4.

    5.

    6.

    2.5 正则表达式表示分隔符

    字符串中包含分隔符,通过正则表达式的形式确定分隔符,并通过分隔符将字符串分割成若干个子字符串。

    在正则表达式中"\d+"表示一个或多个数字,是用于从一堆数字字母以及其它字符组成的字符串中获取非数字字符或字符串。

    1. public class Test {
    2. public static void main(String[] args) {
    3. String str = "2018年11月18日abcd85gg688";
    4. //正则表达式中\d+表示一个或多个数字,java中\\表示一个普通\
    5. String[] data = str.split("\\d+");
    6. for (int i = 0; i < data.length; i++) {
    7. System.out.println(data[i]);
    8. }
    9. }
    10. }

    运行结果:

    日abcd

    gg

    2.6 空格

    字符串中包含空格分隔符,需要通过空格分隔符将字符串分割成若干个子字符串。

    返回字符串中的所有单词,仅不包括空格。

    1. public class Test {
    2. public static void main(String[] args) {
    3. String s1 = "java string split method";
    4. // String[] words = s1.split("\\s");
    5. String[] words = s1.split(" ");//两种方法都可以,根据空格拆分字符串
    6. for (String w : words) {
    7. System.out.println(w);
    8. }
    9. }
    10. }

    运行结果:

    java

    string

    split

    method

    2.7 多个分隔符

    字符串中包含多种分隔符,需要通过多种分隔符将字符串分割成若干个子字符串。

    方式1.多个分隔符用引号括起来

    方式2.多个分隔符用引号和中括号括起来

    1. public class Test {
    2. public static void main(String[] args) {
    3. String str = "2021年11月18日;英语,数学,语文;";
    4. // 方式1.多个分隔符用引号括起来,并且用“|”进行分割
    5. // String[] data = str.split(",|;");
    6. // 方式2.多个分隔符用引号和中括号括起来
    7. String[] data = str.split("[,;]");
    8. for (int i = 0; i < data.length; i++) {
    9. System.out.println(data[i]);
    10. }
    11. }
    12. }

    运行结果:

    2021年11月18日

    英语

    数学

    语文

    2.8 大写字母拆分字符串

    字符串中包含大写字母,需要通过将大写字母作为分隔符,将字符串分割成若干个子字符串。

    1. public class Test {
    2. public static void main(String[] args) {
    3. String s = "AB#K$bb";
    4. String[] str = s.split("(?=\\p{Lu})");
    5. System.out.println("Number of substrings: " + str.length);
    6. for (int i = 0; i < str.length; i++) {
    7. System.out.println("Str[" + i + "]:" + str[i]);
    8. }
    9. }
    10. }

    运行结果:

    Number of substrings: 3

    Str[0]:A

    Str[1]:B#

    Str[2]:K$bb

  • 相关阅读:
    maven_0
    如何在国庆抓住收益红利?这家酒店集团最佳实践值得一看
    【Azure Developer】完成算法第4版书中,第一节基础编码中的数组函数 histogrm()
    SpringBoot + openFeign实现远程接口调用
    Ceres学习笔记004--使用Ceres进行2D圆拟合
    web APIs——第一天(上)
    基于QtGUI的宠物小精灵对战游戏设计
    多线程---JUC
    Go | 基本数据类型的相互转换
    loadrunner-Controller负载测试-各模块功能记录01测试场景设计
  • 原文地址:https://blog.csdn.net/xijinno1/article/details/134084988