Groovy:将路径中的/替换为\

4

如何在 Groovy 中使用 replaceAll 方法,将 / 替换为 \?使得 "//10.1.1.1/temp/test" 变成 "\\10.1.1.1\temp\test"

"//10.1.1.1/temp/test".replaceAll(/'\\/'/,'\\') <-- ? 这种方式不起作用。

有没有任何想法呢?

感谢任何回答。

2个回答

12

看看这个 "//10.1.1.1/temp/test".replaceAll("/","\\\\")"\\\\" 代表一个反斜杠。


1
在迭代器中要注意toString方法...f = new File('c:/temp/test/') f.eachFileMatch { it.split("\.")[1].length()==3 } { println it.toString().replaceAll("\\\\","/") } - Booyeoo

-1
请检查这个。
 String path = 'D:/folder1/folder2/yourfile' 
 String result = path.replaceAll( "/","\\");

最终你会得到类似的结果。
'D:\folder1\folder2\yourfile' 

无法运行:java.lang.IllegalArgumentException: 缺少要转义的字符。 - Hugo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接