aspose file tools*
The moose likes Beginning Java and the fly likes String Replace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Replace" Watch "String Replace" New topic
Author

String Replace

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Hi

Am facing a small in String Replace ALL method. Some please assist me to resolve the issue.




Out put is : C:test d:\test\


Am expecting the url to be C\:test but it's coming as C:test

If I add String url="C:\\\\test"; then its working fine. But in my case am getting this url dynamically from another class. So I can't go for this option. Please help me.


Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Can I try like this. This is working fine. But I guess it's a worst option.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Far better to use a StringBuilder for that sort of manipulation. You can iterate through the String, look for an instance of \ and insert a \\ character after it. Just make sure to increment your index after the insertion, otherwise you will get strings with look like th\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\is
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Replace