如何获得角色的“英文名字”?

时间:2012-01-19 18:29:17

标签: c# .net char character

我刚刚使用了这个最实用的链接:How do I check if a given string is a legal / valid file name under Windows?

在一些验证代码中我有一些看起来像的东西(忽略我不使用StringBuilder类并忽略形成消息的错误的事实(不需要多次告诉他们'冒号'如果它不止一次出现在字符串中)):

string InvalidFileNameChars = new string(Path.GetInvalidFileNameChars());
Regex ContainsABadChar = new Regex("[" + Regex.Escape(InvalidFileNameChars) + "]");

MatchCollection BadChars = ContainsABadChar.Matches(txtFileName.Text);
if (BadChars.Count > 0)
{
    string Msg = "The following invalid characters were detected:\r\n\r\n";
    foreach (Match Bad in BadChars)
    {
        Msg += Bad.Value + "\r\n";
    }
    MessageBox.Show(Msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

MessageBox看起来像(使用找到冒号的例子):

- 开始 -

检测到以下无效字符:

- 结束 -

我希望它能说:

- 开始 -

检测到以下无效字符:

冒号 - > :

- 结束 -

我喜欢英文名字。不是杀手,但很奇怪是否有一些功能(Char类不存在,但可能存在于其他一些我没想到的类中):

Char.GetEnglishName( ':');

4 个答案:

答案 0 :(得分:6)

如果您不需要为每个角色负责,您可以使用basic latin and controls unicode block

您可以将表定义为一个简单的字符串数组,以便快速查找:

string[] lookup = new string[128];
lookup[0x00]="Null character";
lookup[0x01]="Start of Heading";
lookup[0x02]="Start of Text";
lookup[0x03]="End-of-text character";
lookup[0x04]="End-of-transmission character";
lookup[0x05]="Enquiry character";
lookup[0x06]="Acknowledge character";
lookup[0x07]="Bell character";
lookup[0x08]="Backspace";
lookup[0x09]="Horizontal tab";
lookup[0x0A]="Line feed";
lookup[0x0B]="Vertical tab";
lookup[0x0C]="Form feed";
lookup[0x0D]="Carriage return";
lookup[0x0E]="Shift Out";
lookup[0x0F]="Shift In";
lookup[0x10]="Data Link Escape";
lookup[0x11]="Device Control 1";
lookup[0x12]="Device Control 2";
lookup[0x13]="Device Control 3";
lookup[0x14]="Device Control 4";
lookup[0x15]="Negative-acknowledge character";
lookup[0x16]="Synchronous Idle";
lookup[0x17]="End of Transmission Block";
lookup[0x18]="Cancel character";
lookup[0x19]="End of Medium";
lookup[0x1A]="Substitute character";
lookup[0x1B]="Escape character";
lookup[0x1C]="File Separator";
lookup[0x1D]="Group Separator";
lookup[0x1E]="Record Separator";
lookup[0x1F]="Unit Separator";
lookup[0x20]="Space";
lookup[0x21]="Exclamation mark";
lookup[0x22]="Quotation mark";
lookup[0x23]="Number sign";
lookup[0x24]="Dollar sign";
lookup[0x25]="Percent sign";
lookup[0x26]="Ampersand";
lookup[0x27]="Apostrophe";
lookup[0x28]="Left parenthesis";
lookup[0x29]="Right parenthesis";
lookup[0x2A]="Asterisk";
lookup[0x2B]="Plus sign";
lookup[0x2C]="Comma";
lookup[0x2D]="Hyphen-minus";
lookup[0x2E]="Full stop";
lookup[0x2F]="Slash";
lookup[0x30]="Digit Zero";
lookup[0x31]="Digit One";
lookup[0x32]="Digit Two";
lookup[0x33]="Digit Three";
lookup[0x34]="Digit Four";
lookup[0x35]="Digit Five";
lookup[0x36]="Digit Six";
lookup[0x37]="Digit Seven";
lookup[0x38]="Digit Eight";
lookup[0x39]="Digit Nine";
lookup[0x3A]="Colon";
lookup[0x3B]="Semicolon";
lookup[0x3C]="Less-than sign";
lookup[0x3D]="Equal sign";
lookup[0x3E]="Greater-than sign";
lookup[0x3F]="Question mark";
lookup[0x40]="At sign";
lookup[0x41]="Latin Capital letter A";
lookup[0x42]="Latin Capital letter B";
lookup[0x43]="Latin Capital letter C";
lookup[0x44]="Latin Capital letter D";
lookup[0x45]="Latin Capital letter E";
lookup[0x46]="Latin Capital letter F";
lookup[0x47]="Latin Capital letter G";
lookup[0x48]="Latin Capital letter H";
lookup[0x49]="Latin Capital letter I";
lookup[0x4A]="Latin Capital letter J";
lookup[0x4B]="Latin Capital letter K";
lookup[0x4C]="Latin Capital letter L";
lookup[0x4D]="Latin Capital letter M";
lookup[0x4E]="Latin Capital letter N";
lookup[0x4F]="Latin Capital letter O";
lookup[0x50]="Latin Capital letter P";
lookup[0x51]="Latin Capital letter Q";
lookup[0x52]="Latin Capital letter R";
lookup[0x53]="Latin Capital letter S";
lookup[0x54]="Latin Capital letter T";
lookup[0x55]="Latin Capital letter U";
lookup[0x56]="Latin Capital letter V";
lookup[0x57]="Latin Capital letter W";
lookup[0x58]="Latin Capital letter X";
lookup[0x59]="Latin Capital letter Y";
lookup[0x5A]="Latin Capital letter Z";
lookup[0x5B]="Left Square Bracket";
lookup[0x5C]="Backslash";
lookup[0x5D]="Right Square Bracket";
lookup[0x5E]="Circumflex accent";
lookup[0x5F]="Low line";
lookup[0x60]="Grave accent";
lookup[0x61]="Latin Small Letter A";
lookup[0x62]="Latin Small Letter B";
lookup[0x63]="Latin Small Letter C";
lookup[0x64]="Latin Small Letter D";
lookup[0x65]="Latin Small Letter E";
lookup[0x66]="Latin Small Letter F";
lookup[0x67]="Latin Small Letter G";
lookup[0x68]="Latin Small Letter H";
lookup[0x69]="Latin Small Letter I";
lookup[0x6A]="Latin Small Letter J";
lookup[0x6B]="Latin Small Letter K";
lookup[0x6C]="Latin Small Letter L";
lookup[0x6D]="Latin Small Letter M";
lookup[0x6E]="Latin Small Letter N";
lookup[0x6F]="Latin Small Letter O";
lookup[0x70]="Latin Small Letter P";
lookup[0x71]="Latin Small Letter Q";
lookup[0x72]="Latin Small Letter R";
lookup[0x73]="Latin Small Letter S";
lookup[0x74]="Latin Small Letter T";
lookup[0x75]="Latin Small Letter U";
lookup[0x76]="Latin Small Letter V";
lookup[0x77]="Latin Small Letter W";
lookup[0x78]="Latin Small Letter X";
lookup[0x79]="Latin Small Letter Y";
lookup[0x7A]="Latin Small Letter Z";
lookup[0x7B]="Left Curly Bracket";
lookup[0x7C]="Vertical bar";
lookup[0x7D]="Right Curly Bracket";
lookup[0x7E]="Tilde";
lookup[0x7F]="Delete";

然后,您需要做的就是:

var englishName = lookup[(int)'~'];

或者:

 public static string ToEnglishName(this char c)
 {
    int i = (int)c;
    if( i < lookup.Length )
       return lookup[i];
    return "Unknown";
 }

 var name = ':'.ToEnglishName(); // Colon

答案 1 :(得分:4)

您将遇到的问题是您需要能够代表Unicode空间,这将是一个很大的空间。如果你真的想这样做,把this page的内容放到一个字典中然后在char上使用这个扩展方法:

public static string ToName(this char c)
{
    string result = ""; // or "unknown" or null or whatever
    _charToName.TryGetValue(c, out result);
    return result;
}

// ...

string name = c.ToName();

答案 2 :(得分:1)

我编译了一个字典名称字典,我从各种来源收集了一个字符名称,用于搜索unicode字符的个人工具:http://jumpingfishes.com/unicodechars.htm

字典表示为JavaScript数组,包含20,761个定义。随意借用我的JavaScript来创建一个C#字典:
http://jumpingfishes.com/unicodeDescriptions.js

编辑:更好的是,这是我用来生成JavaScript的文本文件。对于生成C#字典,这可能是一个更容易解析的源。它包含十六进制的字符代码,后跟一个制表符,后跟字符描述 http://jumpingfishes.com/unicodeDictionary.txt

答案 3 :(得分:1)

正如@ rik-hemsley

对这个问题Finding out Unicode character name in .Net的回答中提到的那样

现在比以往任何时候都容易,因为nuget中有一个名为Unicode Information

的包

有了这个,你可以打电话:

UnicodeInfo.GetName(character)