如何用下划线替换斜杠

Meh*_*hdi 2 java

可能重复:
提示java.lang.String.replace问题?
在Java中使用string.replace()

为什么“ /”不替换为“ _”?

public static void main(String[] args) throws IOException {
    String file = "A/B";
    file.replaceAll("/", "_");
    System.out.println(file);
}
Run Code Online (Sandbox Code Playgroud)

das*_*ght 5

因为的实例java.lang.String不可变的*replaceAll返回正确的字符串,但是您的程序将其丢弃。如下更改程序,以解决此问题:

file = file.replaceAll("/", "_");
Run Code Online (Sandbox Code Playgroud)


*这是说“不可更改”的一种奇特的方式:"A/B"创建字符串实例后,您将无法再调用任何方法来更改该值。