如何在 Java 中替換 String 中的字元?


String 類的替換方法接受兩個字元,它用 newChar 替換此字串中 oldChar 的所有出現。

示例

 動態演示

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");
      System.out.print("Return Value :" );
      System.out.println(Str.replace('o', 'T'));
      System.out.print("Return Value :" );
      System.out.println(Str.replace('l', 'D'));
   }
}

輸出

Return Value :WelcTme tT TutTrialspTint.cTm
Return Value :WeDcome to TutoriaDspoint.com

更新於:2019 年 7 月 30 日

259 人次觀看

開啟你的 職業生涯

完成課程認證

開始
廣告
© . All rights reserved.