如何將逗號分隔的 Java 字串轉換為陣列。


可以,使用 String.split() 方法來實現。請看下面的示例 −

示例

public class Tester {
   public static void main(String[] args) {
      String text = "This,is,a,comma,seperated,string.";
      String[] array = text.split(",");
      for(String value:array) {
         System.out.print(value + " ");
      }
   }
}

輸出

This is a comma seperated string.

更新於: 24-2 月 - 2020

6 千次瀏覽

開啟你的 職業生涯

透過完成本課程獲取認證

立刻開始
廣告
© . All rights reserved.