如何使用 Java 字符串替换方法?
java 8object oriented programmingprogramming
String 类的 replace() 方法返回一个新字符串,该字符串将所有出现的 oldChar 替换为 newChar。
示例
import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("返回值:" ); System.out.println(Str.replace('o', 'T')); System.out.print("返回值:"); System.out.println(Str.replace('l', 'D')); } }
输出
返回值:WelcTme tT TutTrialspTint.cTm 返回值:WeDcome to TutoriaDspoint.com