java.lang.String:How can we replace the list of characters in a String?

String s = "abc$d!efg:i";

s = s.replace('=', ' ');

s = s.replace(':', ' ');

s = s.replace('$', ' ');

s = s.replace('!', ' ');

I want to check a list of illegal characters(Ex: $ = : ! ) present in the String 's' and replace all the occurance with empty space ' '

I feel difficult to follow the above code style.. Because, If list of illegal characters increased, need to add the code again to check & replace the respective illegal character.

Can we refactor the above code with any other simple way?

Is there any other way to use Map/Set in this above example?

[641 byte] By [RiswanBabua] at [2007-11-27 6:06:29]
# 1

char[] illegalChars;

// enter your illegalChars...

for (int i = 0; i < illegalChars.length; i++) s = s.replace(illegalChars[i], '');

j_shadinataa at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 2
is there any provision to check & replace as below ?!!! s.replace(<list of illegal characters>, ' ');in result, all the occurance of illegal characters must be replaced with ' '
RiswanBabua at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 3
s = s.replaceAll("[=:$!]","");
sabre150a at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks to all! issue resolved. it works well.. :-)
RiswanBabua at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 5
Oops! Guys.. again i came with the pbm here...I want treat the following two characters also as illegal characters: 1)'[' 2)']' But, unable to replace these characters.. Sabre..any idea?!!
RiswanBabua at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 6
s = s.replaceAll("[\\[\\]=:$!]","");
sabre150a at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks! I got the expected result.Sabre.. I hvae a doubt!! "\\" denotes backslash character in a String. How it works in the example which u gave?!! s = s.replaceAll("[\\[\\]=:$!]","");How it is treated here?!!
RiswanBabua at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 8
RTFM http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
thomas.behra at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 9

> RTFM

> http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

That document is almost perfectly useless for someone who is just starting to learn regexes. What they need is a good tutorial, and the best one I know of for Java programmers is the one at [url=http://www.regular-expressions.info/]this site[/url]. When you're ready to move on from introductory tutorials, get the latest edition of [url=http://www.amazon.com/exec/obidos/ASIN/0596002890/jgsbookselection]Mastering Regular Expressions[/url], by Jeffrey Friedl.

uncle_alicea at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 10
use a regex indeed, you can also find more info about regex in general on: http://www.regular-expressions.info/
cappelleha at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 11

> > RTFM

> >

> http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

>

> That document is almost perfectly useless for someone

> who is just starting to learn regexes. What they

> need is a good tutorial, and the best one I know of

> for Java programmers is the one at

> [url=http://www.regular-expressions.info/]this site[/url]. When you're ready to move on from

> introductory tutorials, get the latest edition of

> [url=http://www.amazon.com/exec/obidos/ASIN/0596002890/jgsbookselection]Mastering Regular Expressions[/url], by Jeffrey Friedl.

Dear Uncle,

maybe you want to read that document first before judging its usefulness. And just in case you can't be bothered, the following is an exact quote, which accidently happens to clearly explain the op's question on the usage of \\[\\] in sabre's regex:

Backslashes, escapes, and quoting

The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.

It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct.

Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.

thomas.behra at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...
# 12
I've glanced over them a time or two. ^_^ I was responding more to the rudeness of your reply than to its content.
uncle_alicea at 2007-7-12 16:22:10 > top of Java-index,Java Essentials,Java Programming...