Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String token

 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use this method I've made:

and then call it like this:

/Rene
 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rene! Efficient method!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my modyfied/optimized version:

Jim, thank you for your feedback.
/Rene
[ July 18, 2002: Message edited by: Rene Larsen ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic