使用 Java/Groovy 替换字符串中的反斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6377295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace backslashes in a string using Java/Groovy
提问by djangofan
Trying to get a simple string replace to work using a Groovy script. Tried various things, including escaping strings in various ways, but can't figure it out.
尝试使用 Groovy 脚本进行简单的字符串替换。尝试了各种事情,包括以各种方式转义字符串,但无法弄清楚。
String file ="C:\Test\Test1\Test2\Test3\"
String afile = file.toString() println
"original string: " + afile
afile.replace("\\", "/")
afile.replaceAll("\\", "/") println
"replaced string: " + afile
This code results in:
此代码导致:
original string: C:\Test\Test1\Test2\Test3\
replaced string: C:\Test\Test1\Test2\Test3\
----------------------------
-----------------------------
The answer, as inspired by Sorrow, looks like this:
受 Sorrow 启发,答案如下所示:
// first, replace backslashes
String afile = file.toString().replaceAll("\\", "/")
// then, convert backslash to forward slash
String fixed = afile.replaceAll("//", "/")
采纳答案by Sorrow
replace
returns a different string. In Java String
s cannot be modified, so you need to assign the result of replacing to something, and print that out.
replace
返回不同的字符串。在Java中String
s不能被修改,所以你需要将替换的结果分配给某物,并打印出来。
String other = afile.replaceAll("\\", "/")
println "replaced string: " + other
Edited: as Neftas pointed in the comment, \
is a special character in regex and thus have to be escaped twice.
编辑:正如 Neftas 在评论中指出的那样,\
是正则表达式中的一个特殊字符,因此必须转义两次。
回答by Mike
1) afile.replace(...) doesn't modify the string you're calling it on, it just returns a new string.
1) afile.replace(...) 不会修改您正在调用它的字符串,它只是返回一个新字符串。
2) The input strings (String file ="C:\\Test\\Test1\\Test2\Test3\\";), from Java's perspective, only contain single backslashes. The first backslash is the escape character, then the second backslash tells it that you actually want a backslash.
2) 输入字符串(String file ="C:\\Test\\Test1\\Test2\Test3\\";),从 Java 的角度来看,只包含单个反斜杠。第一个反斜杠是转义字符,然后第二个反斜杠告诉它您实际上想要一个反斜杠。
so
所以
afile.replace("\\", "/");
afile.replaceAll("\\", "/");
should be...
应该...
afile = afile.replace("\", "/");
afile = afile.replaceAll("\", "/");
回答by hoipolloi
If you're working with paths, you're better off using the java.io.File object. It will automatically convert the given path to the correct operating-system dependant path.
如果您正在处理路径,最好使用 java.io.File 对象。它会自动将给定的路径转换为正确的操作系统相关路径。
For example, (on Windows):
例如,(在 Windows 上):
String path = "C:\Test\Test1\Test2\Test3\";
// Prints C:\Test\Test1\Test2\Test3
System.out.println(new File(path).getAbsolutePath());
path = "/Test/Test1/Test2/Test3/";
// Prints C:\Test\Test1\Test2\Test3
System.out.println(new File(path).getAbsolutePath());
回答by Asad Rasheed
String Object is immutable so if you call a method on string object that modifies it. It will always return a new string object(modified). So you need to store the result return by replaceAll() method into a String object.
字符串对象是不可变的,因此如果您调用修改它的字符串对象的方法。它将始终返回一个新的字符串对象(已修改)。所以需要将replaceAll()方法返回的结果存储到String对象中。
回答by Gangnus
In Groovy you can't even write \\
- it is "an unsupported escape sequence". So, all answers I see here are incorrect.
在 Groovy 中,您甚至无法编写\\
- 它是“不受支持的转义序列”。所以,我在这里看到的所有答案都是不正确的。
If you mean one backslash, you should write \\\\
. So, changing backslashes to normal slashes will look as:
如果你的意思是一个反斜杠,你应该写\\\\
. 因此,将反斜杠更改为普通斜杠将如下所示:
scriptPath = scriptPath.replaceAll("\\", "/")
If you want to replace pair backslashes, you should double the effort:
如果要替换成对反斜杠,则应加倍努力:
scriptPath = scriptPath.replaceAll("\\\\", "/")
Those lines are successfully used in the Gradle/Groovy script I have intentionally launched just now once more - just to be sure.
这些行在我刚刚再次有意启动的 Gradle/Groovy 脚本中成功使用 - 只是为了确定。
What is even more funny, to show these necessary eight backslashes "\\\\\\\\" in the normal text here on StackOverflow, I have to use sixteen of them! Sorry, I won't show you these sixteen, for I would need 32! And it will never end...
更有趣的是,为了在 StackOverflow 上的正常文本中显示这八个必要的反斜杠“\\\\\\\\”,我必须使用其中的 16 个!抱歉,我不会向您展示这 16 个,因为我需要 32 个!而且它永远不会结束......
回答by Vishwajeet
In Groovy you can use regex in this way as well:
在 Groovy 中,您也可以这样使用正则表达式:
afile = afile.replaceAll(/(\)/, "/")
println("replaced string: "+ afile)
Note that (as Sorrow said) replaceAll returns the result, doesn't modify the string. So you need to assign to a var before printing.
请注意(正如 Sorrow 所说)replaceAll 返回结果,不修改字符串。所以你需要在打印之前分配给一个 var。
回答by mike rodent
As found here, the best candidate might be the static
Matcher
method:
正如发现here,最好的候选人可能是static
Matcher
方法:
Matcher.quoteReplacement( ... )
According to my experiments this doubles single backslashes. Despite the method name... and despite the slightly cryptic Javadoc: "Slashes ('\') and dollar signs ('$') will be given no special meaning"
根据我的实验,这会使单个反斜杠加倍。尽管方法名称......尽管Javadoc有点神秘:“斜线('\')和美元符号('$')将没有特殊含义”