首页 > 解决方案 > convert unicode(hex) to string in C#

问题描述

Is there any approach to convert Unicode like "U+4E0B" to its equivalent character?

Ex- {\U+FF8D\U+FF9E\U+FF9D\U+FF84\U+FF9E\U+FF97\U+FF72\U+FF9D - \U+4E0B}

any type of help is appreciated!

标签: c#unicodecasting

解决方案


简单的方法是使用Regex.Replace()带有委托的正则表达式 +:

string str = @"\U+FF8D\U+FF9E\U+FF9D\U+FF84\U+FF9E\U+FF97\U+FF72\U+FF9D - \U+4E0BFooBar";

var rx = new Regex(@"\\U\+([0-9A-F]{4})");

string str2 = rx.Replace(str, m =>
{
    ushort u = Convert.ToUInt16(m.Groups[1].Value, 16);
    return ((char)u).ToString();
});

不清楚是否要不区分大小写(因此\u+ff9e有效),然后使用:

var rx = new Regex(@"\\[Uu]\+([0-9A-Fa-f]{4})");

推荐阅读