2

I have to replace all occurrences of ")(" in a string by " ", and then, delete all the occurrences of "(" and ")". For example : (1,1)(2,2) => (1,1 2,2) => 1,1 2,2. I've tried this for the first step but the string wasn't changed:

String test = "(1,1)(2,2)";
test.replaceAll(Pattern.quote(")("), Pattern.quote(" "));
Cœur
  • 34,719
  • 24
  • 185
  • 251
Adrien Varet
  • 365
  • 2
  • 9

1 Answers1

0

Strings are immutable. Simply calling replaceAll does not modify the string, but returns a new one. You need to do

String test = "(1,1)(2,2)";
test = test.replaceAll(Pattern.quote(")("), " ");

or use the replace method not working with regex for simplicity

String test = "(1,1)(2,2)";
test = test.replace(")(", " ");
fabian
  • 72,938
  • 12
  • 79
  • 109