假设以下是我们的字符串。
String str = "Java is a programming language. James Gosling developed it.";
现在,我们将看到如何使用该split()
方法分割字符串。包括定界符作为参数。
String[] strSplit = str.split("\\.");
如上所示,我们在分割方法参数下看到了用点分隔字符串的方法。
以下是一个示例。
public class Demo { public static void main(String[] args) { String str = "Java is a programming language. James Gosling developed it."; System.out.println("String: "+str); String[] strSplit = str.split("\\."); System.out.println("Splitting String..."); for (int i = 0; i < strSplit.length; i++) System.out.println(strSplit[i]); } }
输出结果
String: Java is a programming language. James Gosling developed it. Splitting String... Java is a programming language James Gosling developed it