用其他字符串替换 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.