Java String replace()方法示例。
String类的replace()方法接受两个参数,即-
oldChar-旧字符。
newChar-新字符。
返回一个新的String,该字符串是用newChar替换此字符串中所有出现的oldChar的结果。
示例
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String("Welcome to Nhooo.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