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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Troublesome replaceAll

Status
Not open for further replies.

yodaa

Programmer
Jun 10, 2004
64
SE
Hi,

I'm trying to use java.lang.String.replaceAll() to replace a string that exists within a string with no luck. I'm developing a smaller search engine and want to remove any occurrences of "common words" in query string. The common words are stored in an ArrayList.
The code is as follows
Code:
// Create an iterator (common_words is a public variable)
Iterator it = common_words.iterator();
// Variable to contain the common word
String c_m = new String();
// Do it!
while ( it.hasNext() ) {
    // Common word
    c_m = (String)it.next();
    // Replace common word if it exists
    string.replaceAll(c_m, "");
}
// Return string
return string;

I use the replaceAll() with success if I use to remove the wild-card character some_string.replaceAll("\\*", ""); from some_string. But it wont work with my c_m variable.

Any ideas?
 
Replace :

string.replaceAll(c_m, "");


With :

string = string.replaceAll(c_m, "");


Eg :

Code:
		String commonWord = "cat";
		String line = "A story about a cat in the hat";
		System.out.println(line);

		line = line.replaceAll(commonWord, "");

		System.out.println(line);

--------------------------------------------------
Free Database Connection Pooling Software
 
Hehe, oh my God, how could I not see that one, dizzy me?!

Thank you very much sedj!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top