I would like to remove character “|” by using String#replaceAll() method.
But first parameter is recognized regex meta character.
I tried replaceAll("\|", ""); with escape character, but it cannot be compiled.
Are there any way to remove or replace “|” character by Java?
You need to double-escape the
|when usingreplaceAll(), like:This is because your string actually gets parsed twice, first as a literal string and then as a regular expression. So when you start with
"\\|"the first parse gives you a literal string of\|, which the regex parser then recognizes as|. This can be a bit confusing until you get used to it.