C#根据汉字生成拼音首字母全称
1 static void Main(string[] args) 2 { 3 string s = GetChineseSpell("周杰伦"); 4 Console.WriteLine(s.ToLower()); 5 Console.ReadKey(); 6 } 7 static public string GetChineseSpell(string strText) 8 { 9 int len = strText.Length; 10 string myStr = ""; 11 for (int i = 0; i < len; i++) 12 { 13 myStr += getSpell(strText.Substring(i, 1)); 14 } 15 return myStr; 16 } 17 18 static public string getSpell(string cnChar) 19 { 20 byte[] arrCN = Encoding.Default.GetBytes(cnChar); 21 if (arrCN.Length > 1) 22 { 23 int area = (short)arrCN[0]; 24 int pos = (short)arrCN[1]; 25 int code = (area << 8) + pos; 26 int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 27 }; 28 for (int i = 0; i < 26; i++) 29 { 30 int max = 55290; 31 if (i != 25) max = areacode[i + 1]; 32 if (areacode[i] <= code && code < max) 33 { 34 return Encoding.Default.GetString(new byte[] { (byte)(65 + i) }); 35 } 36 } 37 return "*"; 38 } 39 else return cnChar; 40 }
控制台运行结果为: