简体   繁体   中英

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.

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?

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. (It was a really poor design decision on the part of the Java API team, 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

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.)

Note that the doubling is required as \\ is an escape character for Java string (and character) literals. Note that as replace doesn't treat the inputs as regular expression patterns, there's no need to perform further doubling, unlike replaceAll .

EDIT: You're now getting a FileNotFoundException because there isn't a filename with double backslashes in - what made you think there was? 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 "\\\\". 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)

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. 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. 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. So use 8 backslashes.

The second parameter of the replaceAll method isn't using regular expressions, but it has to be escaped as well. So, you need to escape it once for the Java Compiler and once for the replace method: \\\\\\\\ . This is compiled to two backslashes, which are being interpreted as 1 backslash in the replaceAll method.

2. Replace single backslash to a pair of backslashes (what you asked)

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

Same logic as above.

3. Use replace() instead of 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.

Hopefully, I explained well...

-- Edit: Fixed error, as pointed out by xehpuk --

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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