📜  Java中的Split()字符串方法和例子

📅  最后修改于: 2022-05-13 01:55:01.276000             🧑  作者: Mango

Java中的Split()字符串方法和例子

字符串 split()方法在给定正则表达式的匹配项周围打破给定字符串。根据给定的正则表达式拆分后,此方法返回一个字符串数组。例子:

Input String: 016-78967
Regular Expression: - 
Output : {"016", "78967"}

以下是Java中 split() 方法的两种变体:

1. Public String [ ] split ( String regex, int limit )

参数:

  • regex – 一个定界正则表达式
  • 限制 - 产生的阈值

返回:通过拆分给定的字符串来计算字符串数组。

抛出: PatternSyntaxException – 如果提供的正则表达式的语法无效。

限制参数可以有 3 个值:

  • limit > 0 -如果是这种情况,则模式将最多应用 limit-1 次,结果数组的长度不会超过 n,结果数组的最后一个条目将包含最后一个匹配模式之外的所有输入。
  • limit < 0 -在这种情况下,将尽可能多次应用模式,并且结果数组可以是任意大小。
  • limit = 0 -在这种情况下,将尽可能多次应用模式,结果数组可以是任意大小,并且尾随的空字符串将被丢弃。

以下是它的工作原理:让要拆分的字符串为 – geekss@for@geekss

Regex  Limit  Result
@2{“geekss”, ”for@geekss”}
@5{“geekss”, ”for”, ”geekss”} 
@-2{“geekss”, ”for”, ”geekss”}
s    5{“geek”, ”“, “@for@geek”, “”, “”}
s    -2{“geek”, ” “, ” “, “@for@geek”, “”, “”}
s    0{“geek”, ””, ”@for@geek”}

以下是演示 split() 工作的Java示例代码示例 1:

Java
// Java program to demonstrate working of split(regex,
// limit) with small limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("@", 2);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split(regex,
// limit) with high limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("@", 5);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split(regex,
// limit) with negative limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("@", -2);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split(regex,
// limit) with high limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("s", 5);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split(regex,
// limit) with negative limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("s", -2);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split(regex,
// limit) with 0 limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("s", 0);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "GeeksforGeeks:A Computer Science Portal";
        String[] arrOfStr = str.split(":");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "GeeksforGeeksforStudents";
        String[] arrOfStr = str.split("for");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "Geeks for Geeks";
        String[] arrOfStr = str.split(" ");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "Geekssss";
        String[] arrOfStr = str.split("s");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "GeeksforforGeeksfor   ";
        String[] arrOfStr = str.split("for");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


Java
// Java program to demonstrate working of split()
// using regular expressions
public class GFG {
    public static void main(String args[])
    {
        String str = "word1, word2 word3@word4?word5.word6";
        String[] arrOfStr = str.split("[, ?.@]+");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}


输出
geekss
for@geekss

示例 2:

Java

// Java program to demonstrate working of split(regex,
// limit) with high limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("@", 5);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
geekss
for
geekss

示例 3:

Java

// Java program to demonstrate working of split(regex,
// limit) with negative limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("@", -2);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
geekss
for
geekss

示例 4:

Java

// Java program to demonstrate working of split(regex,
// limit) with high limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("s", 5);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
geek

@for@geek

示例 5:

Java

// Java program to demonstrate working of split(regex,
// limit) with negative limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("s", -2);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
geek

@for@geek

示例 6:

Java

// Java program to demonstrate working of split(regex,
// limit) with 0 limit.
public class GFG {
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String[] arrOfStr = str.split("s", 0);
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
geek

@for@geek

2. public String[] split(字符串正则表达式)

split 方法的这种变体将正则表达式作为参数,并在此正则表达式正则表达式的匹配项周围打破给定的字符串。这里,默认限制为 0。

参数: regex – 分隔正则表达式

返回:通过拆分给定的字符串来计算字符串数组。

抛出: PatternSyntaxException – 如果提供的正则表达式的语法无效。

以下是一些工作示例代码:示例 1:

Java

// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "GeeksforGeeks:A Computer Science Portal";
        String[] arrOfStr = str.split(":");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
GeeksforGeeks
A Computer Science Portal

示例 2:

Java

// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "GeeksforGeeksforStudents";
        String[] arrOfStr = str.split("for");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
Geeks
Geeks
Students

在上面的例子中可以看出,模式/正则表达式“for”被应用了两次(因为“for”在要拆分的字符串中出现了两次)示例 3:

Java

// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "Geeks for Geeks";
        String[] arrOfStr = str.split(" ");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
Geeks
for
Geeks

示例 4:

Java

// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "Geekssss";
        String[] arrOfStr = str.split("s");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
Geek

在上面的示例中,结果数组 arrOfStr 中不包含尾随空字符串。示例 5:

Java

// Java program to demonstrate working of split()
public class GFG {
    public static void main(String args[])
    {
        String str = "GeeksforforGeeksfor   ";
        String[] arrOfStr = str.split("for");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
Geeks

Geeks
   

在上面的示例中,尾随空格(因此不是空字符串)成为结果数组 arrOfStr 中的字符串。示例 6:

Java

// Java program to demonstrate working of split()
// using regular expressions
public class GFG {
    public static void main(String args[])
    {
        String str = "word1, word2 word3@word4?word5.word6";
        String[] arrOfStr = str.split("[, ?.@]+");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}
输出
word1
word2
word3
word4
word5
word6

在上面的示例中,只要遇到集合中指定的任何一个字符,就会分隔单词。