Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ReplaceAll

 
Bradley Wong
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bradley Wong
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic