• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

replacee / with \

 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I itried with replace function...but did not help ...


How did you do it ?
 
Rahul Ba
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it different from what you want ? Please explain what is different.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic