java怎么替换字符串中的字符

lewis 2017-07-05 21次阅读

在Java中,可以使用replace()方法来替换字符串中的字符。replace()方法接受两个参数:要替换的字符和替换后的字符。以下是一个示例:

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        String newStr = str.replace('o', 'x');
        System.out.println(newStr);
    }
}

输出结果为:

Hellx Wxrld

在上面的示例中,replace('o', 'x')将字符串中的所有字符'o'替换为'x'



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。