用另一種字元替換特定字元的 Java 程式設計


使用 replace() 方法用另一個字元替換一個特定的字元。假設以下內容是我們的字串,我們在這裡將空格替換為 $ 字元。

String str1 = "Orange is the new Black!";

現在,使用 replace() 方法用 $ 替換字元

str1.replace(' ', '$');

示例

 線上演示

public class Demo {
   public static void main(String[] args) {
      String str1 = "Orange is the new Black!";
      System.out.println("String: "+str1);
      String str2 = str1.replace(' ', '$');
      System.out.println("Updated string: "+str2);
   }
}

輸出

String: Orange is the new Black!
Updated string: Orange$is$the$new$Black!

更新時間:2020 年 6 月 25 日

529 次觀看

啟動您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.