wood burning stoves 2.0*
The moose likes Java in General and the fly likes ReplaceAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ReplaceAll" Watch "ReplaceAll" New topic
Author

ReplaceAll

Bradley Wong
Greenhorn

Joined: Aug 10, 2001
Posts: 16
I am using jdk1.4, but I cannot get this code to work. No compile errors, but runtime errors. any ideas??? Even fooled around with included escape character '\' but no success though I am not good with the \).
String replacee = "_";
String replacer = "' OR PersonalDetails.City='";
System.out.println("Replacee is: " + replacee);
System.out.println("Replacer is: " + replacer);

city.replaceAll(replacee, replacer);
System.out.println(city)
//this shows that city was not updated as hoped
Bradley Wong
Greenhorn

Joined: Aug 10, 2001
Posts: 16
Still no luck....I even tried removing the "_" and putting "or" instead, so that the code looks like:
String replacee = "OR";
String replacer = "' OR City='";
System.out.println("Replacee is: " + replacee);
System.out.println("Replacer is: " + replacer);

city.replaceAll(replacee, "' OR City='");
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Bradley,
Welcome to JavaRanch. Your problem is that Strings are immutable. The replaceAll() method returns a new String object without changing the called String. So if you do this:

Then it should work as you expect.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Bradley Wong
Greenhorn

Joined: Aug 10, 2001
Posts: 16
THanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ReplaceAll