7 java string replace escaping replaceall
我想从路径字符串中替换空格.我试过下面但似乎没有工作:
String path = "/Users/TD/San Diego";
path=path.replaceAll(" ","\\ ");
System.out.println(path);
Run Code Online (Sandbox Code Playgroud)
目标是转换
"/ Users/TD/San Diego"改为"/ Users/TD/San\Diego"
字符串中的任何其他空间也需要替换为"\"
你可以改变
path = path.replaceAll(" ", "\\ ");
Run Code Online (Sandbox Code Playgroud)
逃避反斜杠
path = path.replaceAll(" ", "\\\\ ");
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到(要求的)
/Users/TD/San\ Diego
Run Code Online (Sandbox Code Playgroud)
另一个选择是使用String.replace喜欢
path = path.replace(" ", "\\ ")
Run Code Online (Sandbox Code Playgroud)
输出相同的.
| 归档时间: |
|
| 查看次数: |
15657 次 |
| 最近记录: |