在 Java 中按照逗號 (,) 分割字串
假設以下字串為我們的字串。
String str = " This is demo text, and demo line!";
要使用逗號分隔字串,請使用 Java 中的 split() 方法。
str.split("[,]", 0);
以下是一個完整的示例。
示例
public class Demo { public static void main(String[] args) { String str = "This is demo text, and demo line!"; String[] res = str.split("[,]", 0); for(String myStr: res) { System.out.println(myStr); } } }
輸出
This is demo text and demo line!
廣告