Java程序使用正则表达式拆分字符串
假设我们有以下字符串。
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) throws Exception { String str = "{Java is a programming language} {James Gosling developed it.}"; String[] strSplit = str.split("[{}]"); System.out.println("Splitting String..."); for (int i = 0; i < strSplit.length; i++) System.out.println(strSplit[i]); } }
输出结果
Splitting String... Java is a programming language James Gosling developed it.