Java replace() 方法

返回到:Java 介绍String类

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

语法

public String replace(char oldChar,
                      char newChar)

参数

  • oldChar — 原字符。
  • newChar — 新字符。

返回值

替换后生成的新字符串。

实例

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'));
	}
}

以上程序执行结果为:

返回值 :hellT
返回值 :heDDo

返回到:Java 介绍String类

作者:terry,如若转载,请注明出处:https://www.web176.com/java_api/19595.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年5月5日
下一篇 2023年5月5日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注