简体   繁体   English

Java的String.replaceAll(…)无法用于\\\\和\\

[英]String.replaceAll(…) of Java not working for \\ and \

I want to convert the directory path from: 我要从以下目录转换目录路径:

C:\Users\Host\Desktop\picture.jpg

to

C:\\Users\\Host\\Desktop\\picture.jpg

I am using replaceAll() function and other replace functions but they do not work. 我正在使用replaceAll()函数和其他替换函数,但是它们不起作用。

How can I do this? 我怎样才能做到这一点?

I have printed the statement , it gives me the one which i wanted ie C:\\Users\\Host\\Desktop\\picture.jpg but now when i pass this variable to open the file, i get this exception why? 我已经打印了该语句,它给了我一个我想要的语句,即C:\\ Users \\ Host \\ Desktop \\ picture.jpg,但是现在当我传递此变量以打开文件时,为什么会出现此异常?

java.io.FileNotFoundException: C:\\Users\\Host\\Desktop\\picture.jpg java.io.FileNotFoundException:C:\\ Users \\ Host \\ Desktop \\ picture.jpg

EDIT: Changed from replaceAll to replace - you don't need a regex here, so don't use one. 编辑:从改变replaceAllreplace -你不必在这里需要一个正则表达式,所以不要使用一个。 (It was a really poor design decision on the part of the Java API team, IMO.) (对于Java API团队IMO来说,这是一个非常糟糕的设计决策。)

My guess (as you haven't provided enough information) is that you're doing something like: 我的猜测 (因为您没有提供足够的信息)是您正在执行以下操作:

text.replace("\\", "\\\\");

Strings are immutable in Java, so you need to use the return value, eg 字符串在Java中是不可变的,因此您需要使用返回值,例如

String newText = oldText.replace("\\", "\\\\");

If that doesn't answer your question, please provide more information. 如果那不能回答您的问题,请提供更多信息。

(I'd also suggest that usually you shouldn't be doing this yourself anyway - if this is to include the information in something like a JSON response, I'd expect the wider library to perform escaping for you.) (我还建议通常无论如何您都不应该自己做-如果要将信息包含在JSON响应之类的内容中,我希望更广泛的库可以为您执行转义。)

Note that the doubling is required as \\ is an escape character for Java string (and character) literals. 注意,由于\\是Java字符串(和字符)文字的转义字符,因此需要加倍。 Note that as replace doesn't treat the inputs as regular expression patterns, there's no need to perform further doubling, unlike replaceAll . 请注意,由于replace不会将输入视为正则表达式模式,因此与replaceAll不同,无需执行进一步的加倍操作。

EDIT: You're now getting a FileNotFoundException because there isn't a filename with double backslashes in - what made you think there was? 编辑:您现在正在获取FileNotFoundException因为没有带双反斜杠的文件名-是什么让您认为有? If you want it as a valid filename, why are you doubling the backslashes? 如果要将其用作有效文件名,为什么要加倍反斜杠?

You have to use : 您必须使用:

String t2 = t1.replaceAll("\\\\", "\\\\\\\\");

or (without pattern) : 或(无模式):

String t2 = t1.replace("\\", "\\\\");

Each "\\" has to be preceeded by an other "\\". 每个“ \\”必须以另一个“ \\”开头。 But it's also true for the preceeding "\\" so you have to write four backslashes each time you want one in regex. 但是对于前面的“ \\”也是如此,因此每次在正则表达式中都需要写四个反斜杠。

In strings \\ is bydefault used as escape character therefore in order to select "\\" in a string you have to use "\\" and for "\\" (ie blackslack two times) use "\\\\". 在字符串中,\\默认情况下用作转义符,因此,为了在字符串中选择“ \\”,必须使用“ \\”,对于“ \\”(即两次blackslack),请使用“ \\\\”。 This will solve your problem and thos will also apply to other symbols also like " 这样可以解决您的问题,并且也将适用于其他符号,例如“

Two explanations: 两种解释:

1. Replace double backslashes to one (not what you asked) 1.将双反斜杠替换为一个(不是您要求的)

You have to escape the backslash by backslashes. 您必须通过反斜杠转义反斜杠。 Like this: 像这样:

String newPath = oldPath.replaceAll("\\\\\\\\", "\\");

The first parameter needs to be escaped twice. 第一个参数需要转义两次。 Once for the Java Compiler and once because you use regular expressions. 一次用于Java编译器,一次因为使用正则表达式。 So you want to replace two backslashes by one. 因此,您想将两个反斜杠替换为一个。 So, since we have to escape a backslash add one backslash. 因此,由于我们必须转义一个反斜杠,所以添加一个反斜杠。 Now you have \\\\ . 现在您有了\\\\ This will be compiled to \\ . 这将被编译为\\ BUT!! 但!! you have to escape the backslash once again because the first parameter of the replaceAll method uses regular expressions. 您必须再次转义反斜杠,因为replaceAll方法的第一个参数使用正则表达式。 So to escape it, add a backslash, but that backslash needs to be escaped, so we get \\\\\\\\ . 因此,要对其进行转义,请添加一个反斜杠,但是该反斜杠需要转义,因此我们得到\\\\\\\\ These for backslashes represents one backslash in the regex. 这些反斜杠代表正则表达式中的一个反斜杠。 But you want to replace the double backslash to one. 但是您想将双反斜杠替换为1。 So use 8 backslashes. 因此,请使用8个反斜杠。

The second parameter of the replaceAll method isn't using regular expressions, but it has to be escaped as well. replaceAll方法的第二个参数不使用正则表达式,但也必须转义。 So, you need to escape it once for the Java Compiler and once for the replace method: \\\\\\\\ . 因此,您需要对Java编译器进行一次转义,并对replace方法进行一次转义: \\\\\\\\ This is compiled to two backslashes, which are being interpreted as 1 backslash in the replaceAll method. 它被编译为两个反斜杠,在replaceAll方法中被解释为1个反斜杠。

2. Replace single backslash to a pair of backslashes (what you asked) 2.将单个反斜杠替换为一对反斜杠(您要求的内容)

String newPath = oldPath.replaceAll("\\\\", "\\\\\\\\");

Same logic as above. 与上述逻辑相同。

3. Use replace() instead of replaceAll() . 3.使用replace()代替replaceAll()

String newPath = oldPath.replace("\\", "\\\\");

The difference is that the replace() method doesn't use regular expressions, so you don't have to escape every backslash twice for the first parameter. 区别在于replace()方法不使用正则表达式,因此您不必为第一个参数两次对每个反斜杠进行转义。

Hopefully, I explained well... 希望我能很好地解释...

-- Edit: Fixed error, as pointed out by xehpuk -- -编辑:已修复错误,如xehpuk所指出-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM