Java replace() 字符替换
java 中 replace() 方法通过用 newChar 字符替换字符串中出现的所有 searchChar 字符,并返回替换后的新字符串。
语法
public String replace(char searchChar, char newChar)
参数
- searchChar -- 原字符。
- newChar -- 新字符。
返回值 : 替换后生成的新字符串。
实例:
public class Main {
public static void main(String args[]) {
String Str = new String("www.catroom.com.cn");
System.out.print("返回值 :" );
System.out.println(Str.replace('o', 'T'));
System.out.print("返回值 :" );
System.out.println(Str.replace('w', 'D'));
}
}
以上程序执行结果为:
返回值 :www.catrTTm.cTm.cn
返回值 :DDD.catroom.com.cn