13

I have a file name which is stored in a String variable path. I tried this:

path = path.replaceAll('\','/') 

but it does not work.

10
  • 1
    You have to escape your backslashes with another \. So in a path, use \\ Commented Oct 31, 2012 at 8:15
  • 1
    This was clear, but please also post any error messages you get to help us find the answer ;) Commented Oct 31, 2012 at 8:16
  • 5
    +1 to compensate for downvotes , as i feel this is a legitimate problem and any newbie can get trapped. Commented Oct 31, 2012 at 8:20
  • 1
    This question seems not to be the "silly question" most of us thought after first reading it. Commented Oct 31, 2012 at 8:38
  • 2
    @all who downvoted in first place. Please see all below answers including mine , most of the answers are confused around the problem. So please look in depth of problem before downvote and not at its simplicity. Regards Commented Oct 31, 2012 at 8:40

7 Answers 7

47

replaceAll() needs Strings as parameters. So, if you write

path = path.replaceAll('\', '/'); 

it fails because you should have written

path = path.replaceAll("\", "/"); 

But this also fails because character '\' should be typed '\\'.

path = path.replaceAll("\\", "/"); 

And this will fail during execution giving you a PatternSyntaxException, because the fisr String is a regular expression (Thanks @Bhavik Shah for pointing it out). So, writing it as a RegEx, as @jlordo gave in his answer:

path = path.replaceAll("\\\\", "/"); 

Is what you were looking for.

To make optimal your core, you should make it independent of the Operating System, so use @Thai Tran's tip:

path = path.replaceAll("\\\\", File.separator); 

But this fails throwing an StringIndexOutOfBoundsException (I don't know why). It works if you use replace() with no regular expressions:

path = path.replace("\\", File.separator); 
Sign up to request clarification or add additional context in comments.

3 Comments

this gives PatternSyntaxException
It works fine for me with: path.replaceAll("\\\\", File.separator);
If throwed me an StringIndexOutOfBoundsException exception under Windows XP (in Spanish locale). Still don't know why.
13

If it is a file path, you should try "File.separator" instead of '\' (in case your application works with Nix platform)

5 Comments

+1 for looking deep in the OP's problem and showing the best answer even for future correctness.
The way it should be , great.
Can you edit to give a code example?
@Thai Any example code on how to use it?
The code would totally depend on the way he created the variable 'path'. If he use the separator, then this escaping problem could be avoid. Draft code could be: path = File.separator + "home" + File.separator + "user" + File.separator + "file.txt";
9

Your path=path.replaceAll('\','/'); will not compile, because you have to escape the backslash,

use path=path.replace('\\','/'); (it will replace all Occrurences, see JavaDoc)

or path=path.replaceAll("\\\\", "/"); (this regex escapes the backslash) ;-)

In the comments there is an explanation, why you need 4 of "\" to make the correct regex for one "\".

4 Comments

docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/… shows that \` is a regular expression construct for backslash. Still dont understand why we need \\\` , explain
yes, a backslash is also a special character in regexes. So in a string, this would be one backslash: "\\", to escape the backslash in a regex, it has to be preceded with a backslash, just as in a string, so you need two backslashes for one backslash in a regex. Two backslashes look like this: "\\\\". Hope this clarified it a bit
ahaaaan... alright.. get it.. cool.. please update your answer with this information. thanks
thanks this works path=path.replaceAll("\\\\","/")
3

You should use the replace method and escape the backslash:

path = path.replace('\\', '/'); 

See documentation:

public String replace(char oldChar, char newChar) 

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

2 Comments

That doesn't compile. You should write replaceAll("\\",
Use [String.replace](docs.oracle.com/javase/7/docs/api/java/lang/…, char)) it will replace all chars, replaceAll takes a regex, and i see a PatternSyntaxException flying around here!
1

As it is a file path you have absolutely no need whatsoever to do this operation at all. Java understands both syntaxes. If you are trying to convert a File to a URL or URI, it has methods to do that.

Comments

-1

the \ is not just some character in java.

it has its significance, some characters when preceeded by \ have a special meaning,

refer here section escape sequence for details

Thus if you want to use just \ in your code, there is an implementation \\ for it.

So replace

path=path.replaceAll("\","/") 

with

path=path.replaceAll("\\","/") 

And this will fail during execution giving you a PatternSyntaxException, because the first String is a regular expression So based on @jlordo answer , this is the way to go

path = path.replaceAll("\\\\", "/"); 

7 Comments

It doesn't compile. replaceAll() needs Strings as parameters.
@J.A.I.L. , yea I posted that way but updated it right before your comment. Thanks for the info though
-1 ?? Whats wrong with the answer??? :-/
replaceAll takes a regex, and this will be a PatternSyntaxException...
@MukulGoel but its still wrong!! see my answer
|
-1
 String s="m/j/"; String strep="\\\\"; String result=s.replaceAll("/", strep); System.out.println(result); 

2 Comments

this will solve your problem...
His problem is that he has a String like "m\j\" and wants to transform it to "m/j/".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.