You need:
str = str.replaceAll("World", "\\\\_\\\\_\\\\_\\\\_\\\\_");
See it.
\
is the escape character in Java strings. So to mean a literal \
you need to escape it with another \
as \\
.
\
is the escape char for the regex engine as-well. So a \\
in Java string will be sent to regex engine as \
which is not treated literally but instead will be used to escape the following character.
To pass \\
to regex engine you need to have \\\\
in the Java string.
Since you are replacing a string (not pattern) with another string, there is really no need for regex, you can use the replace
method of the String
class as:
input = input.replace("World", "\\_\\_\\_\\_\\_");
See it.