wood burning stoves 2.0*
The moose likes Java in General and the fly likes replacee / with \ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "replacee / with \ " Watch "replacee / with \ " New topic
Author

replacee / with \

Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205

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: 14687
    
  16

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: 205

I tried...but did not help..

System.out.println("Str:"+str.replace('/','\\'));
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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: 205
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: 14687
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: replacee / with \
 
Similar Threads
How to replace special characters
Time article on Vajpayee
scjp2
weblogic certification
Where are You ?