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
  • 37,241
  • 25
  • 195
  • 267
Adrien Varet
  • 395
  • 2
  • 10

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
  • 80,457
  • 12
  • 86
  • 114