Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replacee / with \

 
Rahul Ba
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I itried with replace function...but did not help ...

How did you do it ?
 
Rahul Ba
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried...but did not help..

System.out.println("Str:"+str.replace('/','\\'));
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it different from what you want ? Please explain what is different.
 
Jaydeep Vaishnav
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul

I tried and it did replace forward slashes with the backward ones for me.
 
Rahul Ba
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I want \\rahul\\sam/man/bay ------> \\rahul\\sam\man\bay

Thanks in advance.
 
Jaydeep Vaishnav
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic