在 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!

更新日期:2023-09-13

32K+ 瀏覽量

開啟你的職業生涯

透過完成課程來獲得認證

開始
廣告