在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'。