本文介绍了将转义的 Unicode 字符转换回实际字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Java 中的字符串变量中有以下值,它具有如下编码的 UTF-8 字符
I have the following value in a string variable in Java which has UTF-8 characters encoded like below
Doddu2013Frank
代替
Dodd–Frank
(假设我无法控制如何将此值分配给此字符串变量)
(Assume that I don't have control over how this value is assigned to this string variable)
现在如何正确转换(编码)它并将其存储回 String
变量中?
Now how do I convert (encode) it properly and store it back in a String
variable?
我找到了以下代码
Charset.forName("UTF-8").encode(str);
但这会返回一个 ByteBuffer
,但我想要返回一个 String
.
But this returns a ByteBuffer
, but I want a String
back.
编辑:
更多其他信息.
当我使用 System.out.println(str);
我得到
When I use System.out.println(str);
I get
Doddu2013Frank
我不确定什么是正确的术语(UTF-8 或 unicode).请原谅我.
I am not sure what is the correct terminology (UTF-8 or unicode). Pardon me for that.
推荐答案
try
str = org.apache.commons.lang3.StringEscapeUtils.unescapeJava(str);
这篇关于将转义的 Unicode 字符转换回实际字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!