String replace(char oldChar, char newChar)

Java String类Java String类

replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。

语法

public String replace(char oldChar,
                      char newChar)
w)/3)/x)/u)/e.com提供本手册内容,请勿盗用!

参数

返回值

替换后生成的新字符串。

实例

public class Test {
	public static void main(String args[]) {
		String Str = new String("hello");

		System.out.print("返回值 :" );
		System.out.println(Str.replace('o', 'T'));

		System.out.print("返回值 :" );
		System.out.println(Str.replace('l', 'D'));
	}
}
w)/3)/x)/u)/e.com提供本手册内容,请勿盗用!

以上程序执行结果为:

返回值 :hellT
返回值 :heDDo
w)/3)/x)/u)/e.com提供本手册内容,请勿盗用!

Java String类Java String类