Replacing double backslashes with single backslash

2020-02-11 05:39发布

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" from "\\u003c"? I am using java.

I tried with,

myString.replace("\\\\", "\\");

but could not achieve what i wanted.

This is my code,

String myString = FileUtils.readFileToString(file);
String a = myString.replace("\\\\", "\\");
byte[] utf8 = a.getBytes();

// Convert from UTF-8 to Unicode
a = new String(utf8, "UTF-8");
System.out.println("Converted string is:"+a);

and content of the file is

\u003c

7条回答
Emotional °昔
2楼-- · 2020-02-11 06:46

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);
}
查看更多
登录 后发表回答