| Author |
replacee / with \
|
Rahul Ba
Ranch Hand
Joined: Oct 01, 2008
Posts: 203
|
|
Hi,
I am facing 1 problem.. I have follwing string and I want to replace / with \ ....
String str = "\\rahul\\sam/man/bay";
I itried with replace function...but did not help ...Is there any idea..so that I can get String in this way "\\rahul\\sam\man\bay"
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
I itried with replace function...but did not help ...
How did you do it ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Rahul Ba
Ranch Hand
Joined: Oct 01, 2008
Posts: 203
|
|
I tried...but did not help..
System.out.println("Str:"+str.replace('/','\\'));
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
|
Is it different from what you want ? Please explain what is different.
|
 |
Jaydeep Vaishnav
Greenhorn
Joined: Sep 08, 2009
Posts: 16
|
|
Rahul
I tried and it did replace forward slashes with the backward ones for me.
|
 |
Rahul Ba
Ranch Hand
Joined: Oct 01, 2008
Posts: 203
|
|
All,
I want \\rahul\\sam/man/bay ------> \\rahul\\sam\man\bay
Thanks in advance.
|
 |
Jaydeep Vaishnav
Greenhorn
Joined: Sep 08, 2009
Posts: 16
|
|
Rahul
By looking at the output it seems that you want two backward slashes for rahul and sam respectively. Because input string is having only two "\\" (for each of them) the first one behaves as escape character for the second one. (It tells compiler that succeeding \ should be literally considered as backward slash.) And so you end up having only one \ in your output. Can you change i/p string? This is what I thought after looking at your desired output.
String str= "\\\\Rahul\\\\Sam/man/bay";
System.out.println("Str: "+str.replace('/','\\'); -------> \\Rahul\\Sam\man\bay
Will this help?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
I want \\rahul\\sam/man/bay ------> \\rahul\\sam\man\bay
What does System.out.println("\\rahul\\sam/man/bay"); print ? As Jaydeep explained, you are under the false impression that there are two slashes before rahul and sam.
|
 |
 |
|
|
subject: replacee / with \
|
|
|