Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ReplaceRange

Status
Not open for further replies.

eelsar

Technical User
May 20, 2002
36
US
I am having the following problem, I am trying to replace one word with another. It works however sometimes it overlaps the word before it.
Here is the code that I used can someone tell me what is wrong?

String original = new String(text.getText());
String with = new String(textTest.getText());
String myString = new String (textArea.getText());
int start = 0;
int end = 0;
start = myString.indexOf(original);
end = original.length() + start;
textArea.replaceRange(with,start,end);

Thank you!


 
subtract 1 from:

end = (original.length() - 1) + start

because you have started at the first initial position already...
 
Thank you for your response. I tried what you wrote but it doesn't seem to be working.

Using:
end = (original.length() - 1) + start

The results of replacing "quick" with "slow" in the sentence "The quick brown fox" was"
"Theslow k brown fox"

The result of the original way I was doing it was:
"Theslow brown fox"

Do you know what to do? Can you please respond? If anyone else knows please respond.

Thank you!
 
I'd try a StringTokenizer and StringBuffer duo

String str = "I eat pie with both hands";
StringTokenizer st = new StringTokenizer(str);
StingBuffer sb = new StringBuffer();
final String SPACE = ' ';
String temp;

while(st.hasMoreTokens()){
temp = st.getToken();
if(temp.equals("pie")){
sb.append("burgers");
}
else{
sb.append(temp);
}
if(st.hasMoreTokens())
sb.append(SPACE);
}
System.out.println(sb.toString());


i don't know if this will compile, i'm drunk. But you get the idea $:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top