For example...
String s = "abcxyzabcxyz";
s.replaceAll("xyz", "---");
This would normally give me "abc---abc---"
. How would I get it to give me "---xyz---xyz"
instead?
For example...
String s = "abcxyzabcxyz";
s.replaceAll("xyz", "---");
This would normally give me "abc---abc---"
. How would I get it to give me "---xyz---xyz"
instead?
String#replaceAll
accepts a regex, if I understood you correctly, you can use:
s.replaceAll("[^xyz]", "---")
Explanation:
[^xyz]
means any character that's not one of "x", "y" or "z".
In order to negate "xyz" as a series, use:
^(?!.*xyz).*$
You can use this...
s.replaceAll("[^xyz]", "-");
On a more formal Java approach, I would use the Pattern class, like this:
Pattern p = Pattern.compile("[^xyz]");
s.replaceAll(p.pattern(), "-");