如何通过输入汉字输出汉字首字母

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace UnicodeToGb2312
7 {
8     class Program
9     {
10         public static char charFirst(string word)
11         {
12             string[] bt = new string[]
13             {
14             "b0a1","b0c5","b2c1","b4ee","b6ea","b7a2","b8c1","b9fe","bbf7","bfa6","c0ac","c2e8","c4c3","c5b6","c5be","c6da",
15             "c8bb","c8f6","cbfa","cdda","cef4","d1b9","d4d1"
16             };
17
18             char[][] refer = new char[23][];
19             for (int i = 0; i < bt.Length; i++)
20             {
21                 refer[i] = bt[i].ToLower().ToCharArray();
22             }
23             char[] words ={'a','b','c','d','e','f','g','h','j','k','l','m',
24                         'n','o','p','q','r','s','t','w','x','y','z'
25                         };
26
27             byte[] bts = Encoding.GetEncoding("gb2312").GetBytes(word);
28             string[] stringAll = new string[23];
29             double[] intAll = new double[23];
30             char[] compare = (bts[0].ToString("x").PadLeft(2, '0') + bts[1].ToString("x").PadLeft(2, '0')).ToCharArray();
31            
32             string stringTemp = string.Empty;
33             for (int i = 0; i < 4; i++)
34             {
35                 stringTemp += Convert.ToString(Convert.ToInt32(compare[i])).PadLeft(3,'0');
36             }
37             double intTemp = Convert.ToDouble(stringTemp);
38
39             for (int i = 0; i < bt.Length; i++)
40             {
41                 for (int j = 0; j < bt[i].Length; j++)
42                 {
43                     stringAll[i] += Convert.ToInt32(refer[i][j]).ToString().PadLeft(3,'0');
44                 }
45                 intAll[i] = Convert.ToDouble(stringAll[i]);
46             }
47
48             for (int i = 0; i < bt.Length - 1; i++)
49             {
50                 if (intTemp >= intAll[i] && intTemp < intAll[i + 1])
51                 {
52                     return words[i];
53                 }
54
55             }
56             return 'z';
57
58         }
59         static void Main(string[] args)
60         {
61
62
63
64             string s="我爱你中国中国人民欢迎你我是小菜鸟";
65             char[] ss = s.ToArray();
66             for (int i = 0; i < ss.Length; i++)
67             {
68                 string  sss = Convert.ToString(ss[i]);
69                 Console.WriteLine(charFirst(sss));
70             }
71            
72             Console.Read();
73         }
74     }
75 }

原文地址:https://www.cnblogs.com/nvye/p/2772098.html