|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Java newbie here. I wish to open an html document and search for kewords which are to be replaced with other values.
I checked out the String.replace() function which only works on characters. Is there another function I can use? I thought about trying to learn the regular expression functions to do it. Can anyone point me in the right direction? |
|
#2
|
|||
|
|||
|
I figured it out, as follows:
String htmlDoc = "<input name=cmName type=text value=__cmName__>"; String oldString = "__cmName__"; String newString = "Joel Nelson"; int htmlLength = htmlDoc.length(); int oldLength = oldString.length(); int index = htmlDoc.indexOf(oldString.toString()); if (index != -1) { htmlDoc = htmlDoc.substring (0, index) + newString.toString() + htmlDoc.substring (index + oldLength, htmlLength); } |
|
#3
|
|||
|
|||
|
Well, if you have access to jdk 1.4, go ahead and learn the regular expression functions. They're not very difficult, and they're *extremely* useful.
One suggestion that I might make to your code is to put it into a loop. That way all occurence get replaced, not just the first. PHP Code:
__________________
-james |
|
#4
|
|||
|
|||
|
Good point on the looping, bricker!
I actually do know regular expressions, but via perl (my main language). I'm sure there are some differences in usage but they prolly work about the same. I'll check it out!
__________________
There are only 10 kinds of people in this world. Those who understand binary, and those who don't! |
|
#5
|
||||
|
||||
|
yah that works.. and you dont need toString() for all the strings it is default
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Best way to replace string in substring? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|