|
-
Nov 29th, 2004, 12:34 PM
#1
Thread Starter
Addicted Member
How do I....
How do i search a string for a certain word, then replace that word with something else.
For example:
String sentance="This is an example";
String searchword="is";
String replaceword="was";
What I need to do is be able to search sentance for the string "is" and replace it with the string "was"
-
Nov 29th, 2004, 01:20 PM
#2
There's a replace method in java.lang.String.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 29th, 2004, 02:17 PM
#3
Thread Starter
Addicted Member
since I am using strings, should I use the replaceAll() method?
Code:
/**
* Title:Editor project
* @author Matthew Davis
* Class Info 1314
* Date: November 22, 2004
**/
package Editor;
import java.util.StringTokenizer;
public class Editor
{
String document;
String searchText;
String replacementText;
public Editor()
{
}//end constructor
public void setDocument(String recdocument)
{
document=recdocument;
}
public String getDocument()
{
return document;
}
public String replace(String searchText, String replacementText)
{
String [] token = new String[10];
StringTokenizer sTokens = new StringTokenizer(document, ",");
int count = 0;
while(sTokens.hasMoreTokens())
{
token[count++] = sTokens.nextToken().trim();
}
String resultStr = "";
for(int i = 0; i < count; i++)
{
if(token[i].equalsIgnoreCase(searchText))
{
resultStr = resultStr.replaceAll(token[i],replacementText);
}
else
{
resultStr += token[i] + ", ";
}
}
resultStr = resultStr.substring(0, resultStr.trim().length() - 1);
return resultStr.toString();
}
public String replace(String document, String searchText, String replacementText)
{
String [] token = new String[10];
StringTokenizer sTokens = new StringTokenizer(document, ",");
int count = 0;
while(sTokens.hasMoreTokens())
{
token[count++] = sTokens.nextToken().trim();
}
String resultStr = "";
for(int i = 0; i < count; i++)
{
if(token[i].equalsIgnoreCase(searchText))
{
resultStr = resultStr.replaceAll(token[i],replacementText);
}
else
{
resultStr += token[i] + ", ";
}
}
resultStr = resultStr.substring(0, resultStr.trim().length() - 1);
return resultStr.toString();
}
}
Last edited by ddmeightball; Nov 29th, 2004 at 03:12 PM.
-
Nov 29th, 2004, 03:36 PM
#4
Dazed Member
Posted by ddmeightball
since I am using strings, should I use the replaceAll() method?
Yes use the replaceAll(String regex, String replacement) method. replace(char oldChar, char newChar) is just used if you want to replace
a character with another.
Code:
public class ReplaceEx{
public static void main(String[] args){
String s = "Hello my name is bill";
String s2 = s.replaceAll("bill", "Jim");
System.out.println(s);
System.out.println(s2);
}
}
-
Nov 29th, 2004, 04:33 PM
#5
Java 1.5 has a replace(CharSequence, CharSequence) which I would recommend over replaceAll, because it is faster.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 29th, 2004, 04:43 PM
#6
Dazed Member
replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
Aren't they the same?
-
Nov 29th, 2004, 04:57 PM
#7
The latter uses regular expression matching, which allows advanced features, but consequently is far slower than the plain-text matching of the former. Also, you don't have to worry about escaping characters in the former.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|