本文介绍了用单反斜杠替换双反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串\u003c",它属于 UTF-8 字符集.由于存在双反斜杠,我无法将其解码为 un​​icode.我如何从\u003c"获取\u003c"?我正在使用 java.

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.

我试过了,

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

但无法实现我想要的.

这是我的代码,

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);

文件的内容是

u003c

推荐答案

不确定您是否仍在寻找问题的解决方案(因为您已经接受了答案)但我仍然会添加我的答案作为可能的解决方案所述问题:

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: [<]

这里是上面代码的在线演示

这篇关于用单反斜杠替换双反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 02:12