You can use String#replaceAll
:
String str = "\\u003c";
str= str.replaceAll("", "");
System.out.println(str);
It looks weird because the first argument is a string defining a regular expression, and
is a special character both in string literals and in regular expressions. To actually put a
in our search string, we need to escape it (\
) in the literal. But to actually put a
in the regular expression, we have to escape it at the regular expression level as well. So to literally get \
in a string, we need write \\
in the string literal; and to get two literal \
to the regular expression engine, we need to escape those as well, so we end up with \\\\
. That is:
String Literal String Meaning to Regex
????????????????????? ??????????????????????????? ?????????????????
Escape the next character Would depend on next char
\ Escape the next character
\\ \ Literal
\\\\ \\ Literal \
In the replacement parameter, even though it's not a regex, it still treats
and $
specially — and so we have to escape them in the replacement as well. So to get one backslash in the replacement, we need four in that string literal.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…