File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes replacee / with \ Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "replacee / with \ " Watch "replacee / with \ " New topic
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
    
  11

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
    
  11

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
    
  11

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: replacee / with \
 
Similar Threads
How to replace special characters
scjp2
Time article on Vajpayee
weblogic certification
Where are You ?