.NET 中文转缩写拼音

 1 public class CNToSpell
 2     {
 3         /// 汉字转拼音缩写
 4         /// Code By MuseStudio@hotmail.com
 5         /// 2004-11-30
 6         /// 要转换的汉字字符串/// 拼音缩写
 7         public static string GetString(string str)
 8         {
 9             string tempStr = "";
10             foreach (char c in str)
11             {
12                 if ((int)c >= 33 && (int)c <= 126)
13                 {
14                     //字母和符号原样保留
15                     tempStr += c.ToString();
16                 }
17                 else
18                 {
19                     //累加拼音声母
20                     tempStr += GetPYChar(c.ToString());
21                 }
22             }
23             return tempStr;
24         }
25 
26         /// Code By MuseStudio@hotmail.com
27         /// 2004-11-30
28         /// 要转换的单个汉字/// 拼音声母
29         private static string GetPYChar(string c)
30         {
31             byte[] array = new byte[2];
32             array = System.Text.Encoding.Default.GetBytes(c);
33             int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
34             if (i < 0xB0A1) return "*";
35             if (i < 0xB0C5) return "a";
36             if (i < 0xB2C1) return "b";
37             if (i < 0xB4EE) return "c";
38             if (i < 0xB6EA) return "d";
39             if (i < 0xB7A2) return "e";
40             if (i < 0xB8C1) return "f";
41             if (i < 0xB9FE) return "g";
42             if (i < 0xBBF7) return "h";
43             if (i < 0xBFA6) return "g";
44             if (i < 0xC0AC) return "k";
45             if (i < 0xC2E8) return "l";
46             if (i < 0xC4C3) return "m";
47             if (i < 0xC5B6) return "n";
48             if (i < 0xC5BE) return "o";
49             if (i < 0xC6DA) return "p";
50             if (i < 0xC8BB) return "q";
51             if (i < 0xC8F6) return "r";
52             if (i < 0xCBFA) return "s";
53             if (i < 0xCDDA) return "t";
54             if (i < 0xCEF4) return "w";
55             if (i < 0xD1B9) return "x";
56             if (i < 0xD4D1) return "y";
57             if (i < 0xD7FA) return "z";
58             return "*";
59         }
60     }

 

posted on 2015-07-08 14:47  勤劳的Coder  阅读(474)  评论(0编辑  收藏  举报