1

This is the exact opposite of this question.

Basically I need something like:

char c = UnicodeInfo.FromName("EURO SIGN"); // c becomes €

The UnicodeInfo NuGet package metioned in that other question looks promising but it doesn't seem to provide that functionality.

Or am I missing something?

EDIT:

Just for context: I need to parse escape sequences like

"The price is \N{EURO SIGN} 20"
  • 2
    Do you need characters outside the Basic Multilingual Plane? If not, I'd just create a `Dictionary` by asking UnicodeInfo for the name of every character... – Jon Skeet Jun 26 '19 at 13:29
  • You could use reflection technique to get to [UnicodeInfo.UnicodeData](https://github.com/GoldenCrystal/NetUnicodeInfo/blob/e4761995ca316815c71ff26661ae13cbd192920f/UnicodeInformation/UnicodeData.cs) private static field. From there you would enumerate arrays `UnicodeCharacterData` and `UnihanCharacterData`, and compare your input text with their properties `string Name`, `UnicodeNameAlias[] NameAliases` (for the former) and various string properties (like `MandarinReading`) for the latter. – Dialecticus Jun 26 '19 at 14:07
  • There isn't much point in hard-coding the name when you can simply use the character code. To find it, run the Charmap.exe applet and type "euro" in the search box. – Hans Passant Jun 26 '19 at 15:27

1 Answers1

1

You can use the UnicodeInformation library you mention to achieve that. It gives you all the information you need so you can create your own helper class to do the reverse mapping.

Here is an sample class:

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

You can then use it the way you want.

string displayText = UnicodeName.GetDisplayText("EURO SIGN");

The method returns a string because in some cases the return is not a single character.

Aleš Doganoc
  • 11,568
  • 24
  • 40