• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ReplaceAll

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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='");
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THanks!
reply
    Bookmark Topic Watch Topic
  • New Topic