向路径添加双斜杠
Add double slash to a path
方法 System.getProperty("user.dir")
returns 是这样的:
C:\Users\myuser\path
但是在我的程序中我需要有双斜线,像这样:
C:\\Users\\myuser\\path
有添加这些双斜杠的简单方法吗?
使用string.replace
函数。
System.out.println("C:\Users\myuser\path".replace("\", "\\"));
即
System.out.println(System.getProperty("user.dir").replace("\", "\\"));
更新:
String s = "C:\Users\myuser\path";
System.out.println(s.replace("\", "\\\"));
输出:
C:\\Users\\myuser\\path
方法 System.getProperty("user.dir")
returns 是这样的:
C:\Users\myuser\path
但是在我的程序中我需要有双斜线,像这样:
C:\\Users\\myuser\\path
有添加这些双斜杠的简单方法吗?
使用string.replace
函数。
System.out.println("C:\Users\myuser\path".replace("\", "\\"));
即
System.out.println(System.getProperty("user.dir").replace("\", "\\"));
更新:
String s = "C:\Users\myuser\path";
System.out.println(s.replace("\", "\\\"));
输出:
C:\\Users\\myuser\\path