12

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

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

but it does not work.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Francis
  • 603
  • 1
  • 6
  • 17
  • 1
    You have to escape your backslashes with another \. So in a path, use \\ – Austin Oct 31 '12 at 08:15
  • You need to use escape character. – Subir Kumar Sao Oct 31 '12 at 08:16
  • 1
    This was clear, but please also post any error messages you get to help us find the answer ;) – jlordo Oct 31 '12 at 08:16
  • This doesn't deserve its own question. – sp00m Oct 31 '12 at 08:17
  • 5
    +1 to compensate for downvotes , as i feel this is a legitimate problem and any newbie can get trapped. – Mukul Goel Oct 31 '12 at 08:20
  • 1
    This question seems not to be the "silly question" most of us thought after first reading it. – J.A.I.L. Oct 31 '12 at 08:38
  • @MukulGoel a newbie should have searched and found this [http://stackoverflow.com/questions/5596458/string-replace-a-backslash](http://stackoverflow.com/questions/5596458/string-replace-a-backslash) – jlordo Oct 31 '12 at 08:40
  • 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 – Mukul Goel Oct 31 '12 at 08:40
  • @MukulGoel: The question itself is legitimate. My downvote is for saying "this does not work" without any details (compilation error, etc.), despite comments asking for them. – interjay Oct 31 '12 at 09:45
  • @interjay. Got you.. Yup right. – Mukul Goel Oct 31 '12 at 09:49

7 Answers7

46

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);
J.A.I.L.
  • 10,644
  • 4
  • 37
  • 50
13

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

Thai Tran
  • 9,815
  • 7
  • 43
  • 64
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 "\".

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html#sum shows that `\\` is a regular expression construct for backslash. Still dont understand why we need `\\\\` , explain – Mukul Goel Oct 31 '12 at 08:33
  • 2
    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 – jlordo Oct 31 '12 at 08:36
  • ahaaaan... alright.. get it.. cool.. please update your answer with this information. thanks – Mukul Goel Oct 31 '12 at 08:37
  • thanks this works path=path.replaceAll("\\\\","/") – Francis Oct 31 '12 at 08:41
  • @Java_Francis Glad i could help. Please don't forget to mark the best answer as correct ;) – jlordo Oct 31 '12 at 08:44
2

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.

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
  • That doesn't compile. You should write `replaceAll("\\",` – J.A.I.L. Oct 31 '12 at 08:19
  • 2
    Use [String.replace](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char, char)) it will replace all chars, replaceAll takes a regex, and i see a PatternSyntaxException flying around here! – jlordo Oct 31 '12 at 08:22
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.

user207421
  • 305,947
  • 44
  • 307
  • 483
-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("\\\\", "/");
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
-1
   String s="m/j/"; 
   String strep="\\\\";
   String result=s.replaceAll("/", strep);
    System.out.println(result);
ermaverick
  • 11
  • 1