Sla*_*ast 2 java android replace
我试图'\\'
用'/'
java(Android)替换,这似乎不起作用!
String rawPath = filePath.replace("\\\\", "/");
Run Code Online (Sandbox Code Playgroud)
这有什么问题?我已经逃脱了"\"并试图逃避'/'但没有用.原始字符串没有任何反应.
filePath = abc\\xyz(not after escaping two \\, the original string is with two \\)
rawPath = abc \ xyz
expected = abc/xyz
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?(另一个Windows文件到Android路径转换问题)
dac*_*cwe 12
使用String.replace(String, String)
反斜杠时不需要两次转义(即使用时replaceAll
- 它处理正则表达式).所以:
String rawPath = filePath.replace("\\", "/");
Run Code Online (Sandbox Code Playgroud)
或者使用char
版本:
String rawPath = filePath.replace('\\', '/');
Run Code Online (Sandbox Code Playgroud)