This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ReplaceAll

 
Bradley Wong
Greenhorn
Posts: 16
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic