用另一個字串替換 Java 中的字串。


replace() 方法將返回一個新字串,它是用 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 日

370 次瀏覽

啟動你的 職業

透過完成課程取得認證

開始學習
廣告
© . All rights reserved.