• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

String token

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rene! Efficient method!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic