在 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+ 浏览量

开启你的职业生涯

通过完成课程来获得认证

开始
广告
© . All rights reserved.