这是完全相反的这个问题。
基本上我需要这样的东西:
char c = UnicodeInfo.FromName("EURO SIGN"); // c becomes €
在另一个问题中提到的UnicodeInfo NuGet包看起来很有希望,但是它似乎没有提供该功能。
还是我错过了什么?
编辑:
仅出于上下文:我需要解析转义序列,例如
"The price is \N{EURO SIGN} 20"
您可以使用您提到的UnicodeInformation库来实现。它为您提供了所需的所有信息,因此您可以创建自己的帮助器类来进行反向映射。
这是一个示例类:
public static class UnicodeName
{
private static Dictionary<string, string> nameDisplayTextDictionary =
UnicodeInfo.GetBlocks().SelectMany((block) => block.CodePointRange).Select(codePoint => UnicodeInfo.GetCharInfo(codePoint)) // select all character infos
.Where(charInfo => charInfo.Name != null) // filter out the ones that have null names
.ToDictionary(charinfo => charinfo.Name, charInfo => charInfo.GetDisplayText()); // create lookup dictionary to get the display text from the name
public static string GetDisplayText(string unicodeName)
{
if (nameDisplayTextDictionary.ContainsKey(unicodeName))
{
return nameDisplayTextDictionary[unicodeName];
}
else
{
throw new Exception($"Unknown unicode name {unicodeName}");
}
}
}
然后,您可以按需要使用它。
string displayText = UnicodeName.GetDisplayText("EURO SIGN");
该方法返回字符串,因为在某些情况下返回的不是单个字符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句