本文介绍了替换“\\"用“/";在 Java 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在 java(Android) 中用 '/'
替换 '\\'
但这似乎不起作用!
I am trying to replace '\\'
with '/'
in java(Android) and this does not seem to work!
String rawPath = filePath.replace("\\\\", "/");
这有什么问题吗?我已经转义 "\" 并尝试转义 '/' 但没有用.原始字符串没有任何反应.
What is wrong with this ? I have escaped "\" and tried escaping '/' but to no use. Nothing happens to the original string.
filePath = abc\\xyz(not after escaping two \\, the original string is with two \\)
rawPath = abc \ xyz
expected = abc/xyz
这样做的正确方法是什么?(另一个 Windows 文件到 Android 路径转换问题)
Whats the correct way of doing this? (Another Windows file to Android path conversion prob)
推荐答案
当使用 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('\\', '/');
这篇关于替换“\\"用“/";在 Java 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!