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 token 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 token" Watch "String token" New topic
Author

String token

Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
IF I have a long html string like
String code = "<table cellspacing=\"0\" border=\"0\" cellpadding=\"5\"> <tr> <td class=\"headingImageBox\" align=\"center\"><b>2000 GMC Yukon XL</b> </td> </tr> <tr> <td align=center class=\"contentImageBox\">First Row<br> <img src=\"../../../images/2000-gmc-suburban/1strow/main_ip_im.jpg\" width=350 usemap=\"#mainip2000-gmc-suburban\" border=\"0\"><br> <a href=\"#top\"><img src=\"../../../html_images/top_of_page.gif\" width=\"100\" height=\"19\" border=\"0\"></a><map name=\"mainip2000-gmc-suburban\"><area shape=\"rect\" coords=\"82,109,111,138\" href=\"#steering\" target=\"_self\"><area shape=\"rect\" coords=\"168,88,197,117\" href=\"#stereo\" target=\"_self\"><area shape=\"rect\" coords=\"263,92,292,121\" href=\"#gl "
i wanna write a method to replace all the gif to jjpg. What's the most efficient way to do that?
Thanks!
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Here's my guess -- not guaranteed to be most efficient:
1- Convert to a StringBuffer(String creation is very expensive -- much more efficient to do string manipulation in a StringBuffer)
2- Search through the String for the location of .gif with String.indexOf(String asdf) -- because .jpg and .gif have the same # of characters -- the location will be the same after you start substituting things
3- use the StringBuffer.replace(int start, int end, String replace) to swap the characters.
thoughts?


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

If you are using JDK 1.4.x then there is a new String method - 'replaceAll' - try read this link:
http://java.sun.com/j2se/1.4/docs/api/java/lang/String.html
/Rene
[ July 17, 2002: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
I am using 1.3 instead.
How about if I wanna change to jpeg instead of jpg which have different char?
Thanks so much for help
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You can use this method I've made:

and then call it like this:

/Rene
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
Thanks Rene! Efficient method!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ermmm... I can't help noticing that a new StringBuffer is being created each time through the loop. And the first part of the input string is getting re-searched for the target substring on every iteration as well. Once you've replaced all occurances of the target up to a given index currentPos, you really only need to search the remaining string for additional occurances:
int index = srcStr.indexOf(oldStr, currentPos);
Here's my version (for when Java 1.4 is unavailable):

[ July 18, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Here is my modyfied/optimized version:

Jim, thank you for your feedback.
/Rene
[ July 18, 2002: Message edited by: Rene Larsen ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String token