In Java, when I do:
"a/b/c/d".replaceAll("/", "@");
I get back
a@b@c@d
But when I do:
"a/b/c/d".replaceAll("/", File.separator);
It throws a StringIndexOutOfBoundsException, and I don’t know why. I tried looking this up, but it wasn’t very helpful. Can anyone help me out?
It says it right there in the documentation:
And, in
Matcher.replaceAll:What you need to do is to escape any escape characters you have in the replacement string, such as with
Matcher.quoteReplacement():Note, I’m using the literal
\\inseprather than usingFile.separatordirectly since my separator is the UNIX one – you should be able to just use:This outputs:
as expected.