The first argument to replaceAll()
expects a regex. The character |
is a control character in regex. To make the regex parser understand that you “literally” mean |
, you should pass \|
to the regex parser. But \
is a control character in Java. To make Java understand that you mean to pass \|
to the regex parser, you should pass \\|
as the first argument to the replaceAll()
method.
Of course, you could also just use the replace()
method, which does not interpret the first argument as regex: serverList.replace("|", "\""")
(or serverList.replace('|','"')
in this particular case) will return the desired string.
solved Replacing all of a type of character in a string with another character [closed]