PDA

View Full Version : [java] string.replace



mickey
7th September 2010, 03:01
Hello,
is there any change, having a String to replace within it all occurence of "[" and "]" ?
This works


mystring = mystring.replace("[", "") ;
mystring = mystring.replace("]", "") ;

but I hope there is the way to do it in one shot; is it there?
thanks.

saa7_go
7th September 2010, 07:17
Yes. Use regular expression.

aamer4yu
7th September 2010, 07:47
Not exactly..but these statements can do -
mystring.replace("[","").replace("]","");
mystring.remove("[").remove("]");

saa7_go
7th September 2010, 08:04
These codes work for me.



String mystring = "123[56]";
mystring = mystring.replaceAll("[\\[\\]]", "");
System.out.println(mystring);