欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 全角半角转换(英文数字假名特殊符号)

程序员文章站 2022-05-26 17:55:15
...
 public static string FullToHalf(this string str)
        {
            var strRet = str;

            // 数値
            foreach (var item in GetHalfFullNumTable())
            {
                var half = item.Key;
                var full = item.Value;
                strRet = strRet.Replace(full, half);
            }
			// 字母
            foreach (var item in GetHalfFullAlphabetTable())
            {
                var half = item.Key;
                var full = item.Value;
                strRet = strRet.Replace(full, half);
            }
			// 假名
            foreach (var item in GetHalfFullKanaTable())
            {
                var half = item.Key;
                var full = item.Value;
                strRet = strRet.Replace(full, half);
            }
			// 符号
            foreach (var item in GetHalfFullSymbolTable())
            {
                var half = item.Key;
                var full = item.Value;
                strRet = strRet.Replace(full, half);
            }
            // 特殊符号
            strRet = strRet.Replace("ー", "-");
            strRet = strRet.Replace("‐", "-");
            strRet = strRet.Replace("―", "-");
            return strRet;
        }
public static Dictionary<string, string> GetHalfFullNumTable()
        {
            return new Dictionary<string, string>()
             {
     {"0" ,  "0"},
     {"1" ,  "1"},
     {"2" ,  "2"},
     {"3" ,  "3"},
     {"4" ,  "4"},
     {"5" ,  "5"},
     {"6" ,  "6"},
     {"7" ,  "7"},
     {"8" ,  "8"},
     {"9" ,  "9"}, 
             };
        }
public static Dictionary<string, string> GetHalfFullAlphabetTable()
        {

            return new Dictionary<string, string>()
             {
                  {"a" ,  "a"},{"b" ,  "b"},{"c" ,  "c"},{"d" ,  "d"},{"e" ,  "e"},{"f" ,  "f"},
                  {"g" ,  "g"},{"h" ,  "h"},{"i" ,  "i"},{"j" ,  "j"},{"k" ,  "k"},{"l" ,  "l"},
                  {"m" ,  "m"},{"n" ,  "n"},{"o" ,  "o"},{"p" ,  "p"},{"q" ,  "q"},{"r" ,  "r"},
                  {"s" ,  "s"},{"t" ,  "t"},{"u" ,  "u"},{"v" ,  "v"},{"w" ,  "w"},{"x" ,  "x"},
                  {"y" ,  "y"},{"z" ,  "z"},
                  {"A" ,  "A"},{"B" ,  "B"},{"C" ,  "C"},{"D" ,  "D"},{"E" ,  "E"},{"F" ,  "F"},
                  {"G" ,  "G"},{"H" ,  "H"},{"I" ,  "I"},{"J" ,  "J"},{"K" ,  "K"},{"L" ,  "L"},
                  {"M" ,  "M"},{"N" ,  "N"},{"O" ,  "O"},{"P" ,  "P"},{"Q" ,  "Q"},{"R" ,  "R"},
                  {"S" ,  "S"},{"T" ,  "T"},{"U" ,  "U"},{"V" ,  "V"},{"W" ,  "W"},{"X" ,  "X"},
                  {"Y" ,  "Y"},{"Z" ,  "Z"},
             };
        }
public static Dictionary<string, string> GetHalfFullKanaTable()
        {
            return new Dictionary<string, string>()
             {
  {"ア", "ア"}, {"イ", "イ"}, {"ウ", "ウ"}, {"エ", "エ"}, {"オ", "オ"},
  {"カ", "カ"}, {"キ", "キ"}, {"ク", "ク"}, {"ケ", "ケ"}, {"コ", "コ"},
  {"サ", "サ"}, {"シ", "シ"}, {"ス", "ス"}, {"セ", "セ"}, {"ソ", "ソ"},
  {"タ", "タ"}, {"チ", "チ"}, {"ツ", "ツ"}, {"テ", "テ"}, {"ト", "ト"},
  {"ナ", "ナ"}, {"ニ", "ニ"}, {"ヌ", "ヌ"}, {"ネ", "ネ"}, {"ノ", "ノ"},
  {"ハ", "ハ"}, {"ヒ", "ヒ"}, {"フ", "フ"}, {"ヘ", "ヘ"}, {"ホ", "ホ"},
  {"マ", "マ"}, {"ミ", "ミ"}, {"ム", "ム"}, {"メ", "メ"}, {"モ", "モ"},
  {"ヤ", "ヤ"}, {"ユ", "ユ"}, {"ヨ", "ヨ"},
  {"ラ", "ラ"}, {"リ", "リ"}, {"ル", "ル"}, {"レ", "レ"}, {"ロ", "ロ"},
  {"ワ", "ワ"}, {"ヲ", "ヲ"}, {"ン", "ン"},
  {"ヴ", "ヴ"},
  {"ガ", "ガ"}, {"ギ", "ギ"}, {"グ", "グ"}, {"ゲ", "ゲ"}, {"ゴ", "ゴ"},
  {"ザ", "ザ"}, {"ジ", "ジ"}, {"ズ", "ズ"}, {"ゼ", "ゼ"}, {"ゾ", "ゾ"},
  {"ダ", "ダ"}, {"ヂ", "ヂ"}, {"ヅ", "ヅ"}, {"デ", "デ"}, {"ド", "ド"},
  {"バ", "バ"}, {"ビ", "ビ"}, {"ブ", "ブ"}, {"ベ", "ベ"}, {"ボ", "ボ"},
  {"パ", "パ"}, {"ピ", "ピ"}, {"プ", "プ"}, {"ペ", "ペ"}, {"ポ", "ポ"},
  {"ァ", "ァ"}, {"ィ", "ィ"}, {"ゥ", "ゥ"}, {"ェ", "ェ"}, {"ォ", "ォ"},
  {"ッ", "ッ"},
  {"ャ", "ャ"}, {"ュ", "ュ"}, {"ョ", "ョ"},
             };
        }
public static Dictionary<string, string> GetHalfFullSymbolTable()
        {
            return new Dictionary<string, string>()
             {
        {"!", "!"},        {"#", "#"},        {"$", "$"},        {"%", "%"},
        {"&", "&"},        {"'", "'"},        {"(", "("},        {")", ")"},
        {"*", "*"},        {"+", "+"},        {",", ","},        {"-", "-"},
        {".", "."},        {"/", "/"},        {":", ":"},        {";", ";"},
        {"<", "<"},        {"=", "="},        {">", ">"},        {"?", "?"},
        {"@", "@"},        {"[", "["},        {"]", "]"},        {"」", "」"},
        {"^", "^"},        {"_", "_"},        {"`", "`"},        {"{", "{"},
        {"|", "|"},        {"}", "}"},        {"~", "~"},        {" ", " "},
        {"、", "、"},        {"。", "。"},        {"・", "・"},        {"「", "「"},
             };
        }
相关标签: c#