Java教程

Java String split()

Java String split()

在本教程中,我们将通过示例了解 Java String split() 方法。
split() 方法在指定的正则表达式处分割字符串并返回子字符串数组。

示例

class Main {
  public static void main(String[] args) {
    String text = "Java is a fun programming language";
    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)
这里, stringString 类的对象。

split() 参数

字符串 split()方法可以带两个参数:
regex-字符串在这个正则表达式处被分割(可以是字符串) limit(可选)-控制结果子串的数量
如果没有传递 limit参数, split()返回所有可能的子串。

split() 返回值

返回子字符串数组
注意: 如果传递给 split() 的正则表达式无效,split() 方法会引发 PatternSyntaxExpression 异常。

示例一: 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参数,返回的数组包含了所有的子串。

split() 带限制参数

如果 limit 参数为 0 或负数,split() 返回一个包含所有子字符串的数组。 如果 limit 参数为正数(比如 n),split() 返回 n 的最大值> 子字符串。

示例二: 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]
这里,要在 + 处拆分字符串,我们使用了 \\+。这是因为 + 是一个特殊字符(在正则表达式中有特殊含义)。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4