java replace()函数的用法

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('h', 'c'));
}
}

运行结果为:

返回值 :cello



版权声明:本文为weixin_36583895原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。