Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 9th, 2002, 02:22 PM
noslenj123 noslenj123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Phoenix, AZ
Posts: 484 noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 22 h 6 m 30 sec
Reputation Power: 30
Question Best way to replace string in substring?

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?

Reply With Quote
  #2  
Old December 9th, 2002, 04:10 PM
noslenj123 noslenj123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Phoenix, AZ
Posts: 484 noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 22 h 6 m 30 sec
Reputation Power: 30
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);
}

Reply With Quote
  #3  
Old December 10th, 2002, 11:26 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
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:
 String htmlDoc "<input name=cmName type=text value=__cmName__>";
String find "__cmName__";
String replace "Joel Nelson";

int index = -1;

while ( 
index htmlDoc.indexOfoldString.toString() ) > -) {
    
htmlDoc htmlDoc.substring0index ) + replace htmlDoc.substringindex find.length(), htmlDoc.length() );

__________________
-james

Reply With Quote
  #4  
Old December 11th, 2002, 05:24 PM
noslenj123 noslenj123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Phoenix, AZ
Posts: 484 noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 22 h 6 m 30 sec
Reputation Power: 30
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!

Reply With Quote
  #5  
Old December 12th, 2002, 01:11 AM
Shocka's Avatar
Shocka Shocka is offline
dont click here
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 409 Shocka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 35 m
Reputation Power: 7
yah that works.. and you dont need toString() for all the strings it is default

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Best way to replace string in substring?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway