Java 中的模式拆分(CharSequence)方法,示例

原文:https://www . geesforgeks . org/pattern-splitcharsequence-method-in-Java-with-examples/

一个模式类的 split(CharSequence) 方法,用于分割作为参数传递给该模式匹配项周围的方法的给定 char 序列。这个方法可以将 charSequence 拆分成一个字符串数组,使用用于编译模式的正则表达式作为分隔符。

语法:

public String[] split(CharSequence input)

参数:该方法接受单个参数输入,表示待拆分的字符序列。

返回值:该方法返回字符串数组,该数组是通过围绕该模式的匹配项拆分输入来计算的。

以下程序说明了拆分(CharSequence)方法: 程序 1:

// Java program to demonstrate
// Pattern.split(CharSequence) method

import java.util.regex.*;

public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "ee";

        // create the string
        // in which you want to search
        String actualString
            = "geeksforgeeks";

        // create a Pattern using REGEX
        Pattern pattern = Pattern.compile(REGEX);

        // split the text
        String[] array = pattern.split(actualString);

        // print array
        for (int i = 0; i < array.length; i++) {
            System.out.println("array[" + i
                               + "]=" + array[i]);
        }
    }
}

Output:

array[0]=g
array[1]=ksforg
array[2]=ks

程序 2:

// Java program to demonstrate
// Pattern.split(CharSequence) method

import java.util.regex.*;

public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "ke";

        // create the string
        // in which you want to search
        String actualString
            = "Bharat ke Veer Portal";

        // create a Pattern using REGEX
        Pattern pattern = Pattern.compile(REGEX);

        // split the text
        String[] array = pattern.split(actualString);

        // print array
        for (int i = 0; i < array.length; i++) {
            System.out.println("array[" + i
                               + "]=" + array[i]);
        }
    }
}

Output:

array[0]=Bharat 
array[1]= Veer Portal

参考:https://docs . Oracle . com/javase/10/docs/API/Java/util/regex/pattern . html # split(Java . lang . charsequence)