String replaceAll() question!

Hello,

i'm using the following to replace two characters from my input string

String str;//some input string

str = str.replaceAll("a","bc");

str = str.replaceAll("x","yz");

Is there a way to do this in single command (using String'sreplace() or

replaceAll()... or something else)?

Kind regards!

[495 byte] By [igor_ba] at [2007-11-27 5:05:21]
# 1

just write that:

String str; //some input string

str = str.replaceAll("a", "bc").replaceAll("x", "yz");

It is so simple.

Good Luck.

Ahmad Elsafty

NourElsaftya at 2007-7-12 10:23:48 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanx for reply, ... but it's still going to iterate over the string twice. I looking for some way to iterate only once over the string and replace each occurrence of char "a" with "bc" and each "x" with "yz".
igor_ba at 2007-7-12 10:23:48 > top of Java-index,Java Essentials,Java Programming...
# 3

> tr = str.replaceAll("a", "bc").replaceAll("x","yz");

If the OP needs something more sophisticated than this then he should look at

http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html

sabre150a at 2007-7-12 10:23:48 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanx for reply... i'll check it out.
igor_ba at 2007-7-12 10:23:48 > top of Java-index,Java Essentials,Java Programming...