File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String replace for URLs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String replace for URLs" Watch "String replace for URLs" New topic
Author

String replace for URLs

Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 145
I need to replace a URL inside a text file.I have taken the contents of the text file into a String variable "content".
The example code is as below :-

String urlToReplace = "/en/US/products/afile.html" ;
String newUrl = "http://myserver/LA/es/customer/afile.html" ;

String content1 = content.replace(urlToReplace,newUrl) ;

The above statement results in replacing only "afile.html" part of the variable urlToReplace with the whole of newUrl.
So content1 now has "/en/US/products/http://myserver/LA/es/customer/afile.html" instead of expected "http://myserver/LA/es/customer/afile.html" .As you can see only the part after last "/" is getting matched and replaced.Even Pattern class usage does the same thing.

What am I doing wrong?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1


I want you to try like this,
Just replace the single '/' by double '//'.

Like



with this new,



Its a silly guess, but give it a try !!


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
If I am not mistaken..

String content1 = content.replace(urlToReplace,newUrl);

means, replace all substrings of "content" that match urlToReplace with newUrl

so what are the contents of "content" here?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Gamini Sirisena wrote:

so what are the contents of "content" here?


Jitesh wrote:
I have taken the contents of the text file into a String variable "content".
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
I think Sagar has it, try this:




To test this, I wrote a quick class as follows. It compiles and runs fine giving you the new URL as the output.



Give it a go.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 145
Guyz,it seems the replace method works as expected.It replaces the whole string as anyone would expect.There is absolutely no catch in that.
I found that there is some other piece of the code which was mingling with URLs and that was what created confusion.I had to spend 8-10 hours debugging this problem and to find out the root cause.

Sorry for wasting your time guyz.Thanks for lending a helping hand.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: String replace for URLs