Java如何替换反斜杠?

在java中,我有一个文件路径,比如’C:\ A \ B \ C’,我希望它改为”C:/ A / B / C’。 如何更换反斜杠?

String text = "C:\\A\\B\\C"; String newString = text.replace("\\", "/"); System.out.println(newString); 

由于您要求使用正则表达式,因此您必须多次转义’\’字符:

 String path = "c:\\A\\B\\C"; System.out.println(path.replaceAll("\\\\", "/")); 

您可以使用String.replace方法执行此操作:

 public static void main(String[] args) { String foo = "C:\\foo\\bar"; String newfoo = foo.replace("\\", "/"); System.out.println(newfoo); } 
 String oldPath = "C:\\A\\B\\C"; String newPath = oldPath.replace('\\', '/'); 

要替换给定字符的所有匹配项:

 String result = candidate.replace( '\\', '/' ); 

此致,西里尔