• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching and replacing text in a string  RSS feed

 
Mushu Wu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a project where I need to prompt the user to enter some text and then I need to replace all instances of "okay" with "oscar". Here's what I have so far.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I can't really write that code for you at the moment. It sounds like you just need to iterate through the string, finding occurences of "okay", and replacing it with "oscar", making adjustments for the size before and after replacements, while you're searching ....
You may want to explore the need of using StringBuffer, since String creations are rather expensive. StringBuffer probably gives you some functions that would make this little bit of yours alot easier than trying to do it with just the String methods and a bunch of pluses.
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a look at the java.util.regex package and the split and replaceAll methods in String (both since 1.4).
here is some code:


cheers
karl
 
Mushu Wu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Karl. What is the significance of \\bokay in the code?
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
the \\b is (i think) called word boundary.
if you run the example, then you will see that the "\\bokay" regex just replaces two of the three occurences (while the "okay" regex replaces all three).
there is a detailed description of the regex syntax somewhere in the javadoc. you should find a link to it either in the reges package or in the javadoc for the replaceAll / split method of Srtring (remember: only from java 1.4)
karl
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regexes are probably a step beyond beginner, so I'm going to move this to the Java in General (Intermediate) forum.
 
Manoj Zachariah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's an alternative way of using the regex.
import java.util.regex.*;
import java.lang.Object;
import java.lang.*;
class pattern
{
public static void main(String[] args)
{
Pattern p = Pattern.compile("one");
Matcher m = p.matcher("one one one");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while(result) {
m.appendReplacement(sb, "four");
result = m.find();
}
m.appendTail(sb);
System.out.println(sb.toString());
}
}
 
Manoj Zachariah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you dont have the import these.
import java.lang.Object;
import java.lang.*;
i was using it for some other purpose, sorry about that!!!
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if using java 1.4, this works:
String newstring = oldstring.replaceAll("okay", "oscar");
Brian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!