如何更换“ \“用“ \\在java中 [英] How to replace " \ " with " \\ " in java
问题描述
我试图将字符串分解为数组,并将 \
替换为 \\
,但不能做到了,我也试过String.replaceAll这样的东西(\,\\);
。我想提供一个JNI路径,它只能以这种方式读取。请有人帮我这个。谢谢。
I tried to break the string into arrays and replace \
with \\
, but couldn't do it, also I tried String.replaceAll something like this ("\","\\");
. I want to supply a path to JNI and it reads only in this way. Can someone help me on this please. Thank you.
推荐答案
不要使用 String.replaceAll
case - 用正则表达式指定,这意味着你需要更多的转义。这应该没问题:
Don't use String.replaceAll
in this case - that's specified in terms of regular expressions, which means you'd need even more escaping. This should be fine:
String escaped = original.replace("\\", "\\\\");
请注意,由于使用了Java字符串文字,反斜杠加倍 - 所以实际这里涉及的字符串是单反斜杠和双反斜杠 - 不是双重和四重。
Note that the backslashes are doubled due to being in Java string literals - so the actual strings involved here are "single backslash" and "double backslash" - not double and quadruple.
替换
适用于简单字符串 - 不涉及正则表达式。
replace
works on simple strings - no regexes involved.
这篇关于如何更换“ \“用“ \\在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!