split() 方法在指定的正则表达式处拆分字符串,并返回一个子字符串数组。
示例
class Main {
public static void main(String[] args) {
String text = "Java is a fun programming language";
// split string from space
String[] result = text.split(" ");
System.out.print("result = ");
for (String str : result) {
System.out.print(str + ", ");
}
}
}
// Output: result = Java, is, a, fun, programming, language,
String split() 语法
字符串 split() 方法的语法是:
string.split(String regex, int limit)
这里,string是String类的一个对象。
split() 参数
字符串 split() 方法可以接受两个参数:
- regex - 字符串在此正则表达式处被拆分(可以是字符串)
- limit(可选)- 控制结果子字符串的数量
如果未传递 limit 参数,split() 将返回所有可能的子字符串。
split() 返回值
- 返回一个子字符串数组
注意: 如果传递给 split() 的正则表达式无效,split() 方法将引发 PatternSyntaxException 异常。
示例 1:不带 limit 参数的 split()
// importing Arrays to convert array to string
// used for printing arrays
import java.util.Arrays;
class Main {
public static void main(String[] args) {
String vowels = "a::b::c::d:e";
// splitting the string at "::"
// storing the result in an array of strings
String[] result = vowels.split("::");
// converting array to string and printing it
System.out.println("result = " + Arrays.toString(result));
}
}
输出
result = [a, b, c, d:e]
在这里,我们在 :: 处拆分字符串。由于未传递 limit 参数,返回的数组包含所有子字符串。
带 limit 参数的 split()
- 如果
limit参数为 0 或负数,split()将返回一个包含所有子字符串的数组。 - 如果
limit参数为正数(比如n),split()将返回最多n个子字符串。
示例 2:带 limit 参数的 split()
// importing Arrays to convert array to string
import java.util.Arrays;
class Main {
public static void main(String[] args) {
String vowels = "a:bc:de:fg:h";
// splitting array at ":"
// limit is -2; array contains all substrings
String[] result = vowels.split(":", -2);
System.out.println("result when limit is -2 = " + Arrays.toString(result));
// limit is 0; array contains all substrings
result = vowels.split(":", 0);
System.out.println("result when limit is 0 = " + Arrays.toString(result));
// limit is 2; array contains a maximum of 2 substrings
result = vowels.split(":", 2);
System.out.println("result when limit is 2 = " + Arrays.toString(result));
// limit is 4; array contains a maximum of 4 substrings
result = vowels.split(":", 4);
System.out.println("result when limit is 4 = " + Arrays.toString(result));
// limit is 10; array contains a maximum of 10 substrings
result = vowels.split(":", 10);
System.out.println("result when limit is 10 = " + Arrays.toString(result));
}
}
输出
result when limit is -2 = [a, bc, de, fg, h] result when limit is 0 = [a, bc, de, fg, h] result when limit is 2 = [a, bc:de:fg:h] result when limit is 4 = [a, bc, de, fg:h] result when limit is 10 = [a, bc, de, fg, h]
注意: split() 方法将正则表达式作为第一个参数。如果你需要使用特殊字符,例如:\、|、^、*、+ 等,你需要转义这些字符。例如,我们需要使用 \\+ 来在 + 处进行拆分。
示例 3:在 + 字符处 split()
// importing Arrays to convert array to string
// used for printing arrays
import java.util.Arrays;
class Main {
public static void main(String[] args) {
String vowels = "a+e+f";
// splitting the string at "+"
String[] result = vowels.split("\\+");
// converting array to string and printing it
System.out.println("result = " + Arrays.toString(result));
}
}
输出
result = [a, e, f]
在这里,为了在 + 处拆分字符串,我们使用了 \\+。这是因为 + 是一个特殊字符(在正则表达式中有特殊含义)。