使用单个反斜杠替换双反斜杠 [英] Replacing double backslashes with single backslash

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

问题描述

我有一个字符串\\\\<,它属于UTF-8字符集。我无法将其解码为unicode,因为存在双反斜杠。如何从\\\\<获取\\\<?我使用的是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("\\\\", "\\");

但无法达到我想要的效果。

but could not achieve what i wanted.

这是我的代码,

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



OUTPUT:



OUTPUT:

Unicode String is: [<]

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆