在 java 中使用反斜杠转义 space
Escape space with backslash in java
我想从路径字符串中替换 spaces。我在下面尝试过,但似乎没有用:
String path = "/Users/TD/San Diego";
path=path.replaceAll(" ","\ ");
System.out.println(path);
目标是转化
"/Users/TD/San Diego" to "/Users/TD/San\ Diego"
字符串中的任何 space 也需要替换为“\”
你可以改变
path = path.replaceAll(" ", "\ ");
转义反斜杠
path = path.replaceAll(" ", "\\ ");
当我这样做时,我得到(请求的)
/Users/TD/San\ Diego
另一种选择是使用 String.replace
like
path = path.replace(" ", "\ ")
输出相同。
建议的解决方案对我不起作用(在 Android Java 中)。
这就是我在多次尝试后得出的结论:
path = path.replace(" ", (char) 92 + " ");
我想从路径字符串中替换 spaces。我在下面尝试过,但似乎没有用:
String path = "/Users/TD/San Diego";
path=path.replaceAll(" ","\ ");
System.out.println(path);
目标是转化
"/Users/TD/San Diego" to "/Users/TD/San\ Diego"
字符串中的任何 space 也需要替换为“\”
你可以改变
path = path.replaceAll(" ", "\ ");
转义反斜杠
path = path.replaceAll(" ", "\\ ");
当我这样做时,我得到(请求的)
/Users/TD/San\ Diego
另一种选择是使用 String.replace
like
path = path.replace(" ", "\ ")
输出相同。
建议的解决方案对我不起作用(在 Android Java 中)。
这就是我在多次尝试后得出的结论:
path = path.replace(" ", (char) 92 + " ");