Possible Duplicate:
Wrong output using replaceall
If I have string:
String test = "replace()thisquotes";
test = test.replaceAll("()", "");
the test result is still: test = "replace()thisquotes"
so () is not replaced.
Any ideas?
Possible Duplicate:
Wrong output using replaceall
If I have string:
String test = "replace()thisquotes";
test = test.replaceAll("()", "");
the test result is still: test = "replace()thisquotes"
so () is not replaced.
Any ideas?
As others have pointed out, you probably want to use String.replace in this case as you don't need regular expressions.
For reference however, when using String.replaceAll, the first argument (which is interpreted as a regular expression) needs to be quoted, preferably by using Pattern.quote:
String test = "replace()thisquotes";
test = test.replaceAll(Pattern.quote("()"), "");
// ^^^^^^^^^^^^^
System.out.println(test); // prints "replacethisquotes"
The first argument of replaceAll function is a regular expression. "(" character is a special character in regular expressions. Use this :
public class Main {
public static void main(String[] args) {
String test = "replace()thisquotes";
test = test.replaceAll("\\(\\)", "");
System.out.println(test);
}
}