且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

替换“\\"用“/";在 Java 中

更新时间:2023-12-05 12:54:40

当使用 String.replace(String, String) 时,反斜杠不需要转义两次(即使用 replaceAll - 它处理正则表达式).所以:

When using String.replace(String, String) the backslash doesn't need to be escaped twice (thats when using replaceAll - it deals with regex). So:

String rawPath = filePath.replace("\\", "/");

或者使用char版本:

String rawPath = filePath.replace('\\', '/');