考虑以下代码:
class Sample{
public static void main(String args[]){
String text1="C:\Documents\User\sample";
String text2=text1.replace('\','/');
System.out.println(text2);
}
}
我有一个输入,其中 text1 正在接收文件路径。现在我想将 text1 中的所有“\”替换为“/”。我已经使用了上面的代码。
首先,显示的错误是:“Illegal Escape Sequence”,这是可以理解的,不是我的问题,因为我正在制作一个 Java EE 应用程序,用户在输入框中输入路径。
但是我无法将“\”更改为“/”,这对于我的代码正常运行至关重要。我无法解决这个问题。我什至尝试过使用 String.replaceAll() 方法,但没有结果。