I have a string \"\\\\u003c\", which belongs to UTF-8 charset. I am unable to decode it to unicode because of the presence of double backslashes. How do i get \"\\u003c\" fr
Try using,
myString.replaceAll("[\\\\]{2}", "\\\\");
Not sure if you're still looking for a solution to your problem (since you have an accepted answer) but I will still add my answer as a possible solution to the stated problem:
String str = "\\u003c";
Matcher m = Pattern.compile("(?i)\\\\u([\\da-f]{4})").matcher(str);
if (m.find()) {
String a = String.valueOf((char) Integer.parseInt(m.group(1), 16));
System.out.printf("Unicode String is: [%s]%n", a);
}
Unicode String is: [<]
Here is online demo of the above code
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.
"\\u003c"
does not 'belong to UTF-8 charset' at all. It is five UTF-8 characters: '\
', '0', '0', '3', and 'c'. The real question here is why are the double backslashes there at all? Or, are they really there? and is your problem perhaps something completely different? If the String "\\u003c"
is in your source code, there are no double backslashes in it at all at runtime, and whatever your problem may be, it doesn't concern decoding in the presence of double backslashes.
This is for replacing the double back slash to single back slash
public static void main(String args[])
{
String str = "\\u003c";
str= str.replaceAll("\\\\", "\\\\");
System.out.println(str);
}
Another option, capture one of the two slashes and replace both slashes with the captured group:
public static void main(String args[])
{
String str = "C:\\\\";
str= str.replaceAll("(\\\\)\\\\", "$1");
System.out.println(str);
}