Get the abbreviation of the first pinyin letter of a Chinese character string
Author:Eve Cole
Update Time:2009-07-01 16:08:11
The title may not be clear, but the functions implemented are as follows: I Love China-WAZG
1. The difference between Chinese characters and English letters
The standard asc table does not contain Chinese characters, because an asc character only has 1 byte, which is 8 bits. The range of numbers that 8 bits can represent. If it is signed, it should be -128-127. If it is unsigned, it should be 0- 255. And we know that a Chinese character should occupy 2 bytes, and the representation range should be -32768-32767, so the asc of Chinese characters, for example, a bit: 11002111,11111101, the character it represents should exceed the range that asc can express , then overflow will occur. Therefore, the asc code of a Chinese character occupying two bytes should be negative.
2.Function implementation
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace ConsoleApplication1
6{
7 class programs
8 {
9 static void Main(string[] args)
10 {
11 Console.WriteLine(GetChineseFirstChar("I a*% love you China"));;
12}
13 static string GetChineseFirstChar(string chineseStr)
14 {
15 StringBuilder sb = new StringBuilder();
16 int length = chineseStr.Length;
17 for (int i = 0; i < length; i++)
18 {
19 char chineseChar = chineseStr[i];
20 sb.Append(GetpyChar(chineseChar));
twenty one }
22 return sb.ToString();
twenty three }
24 static string GetpyChar(char c)
25 {
26 int ascCode = Microsoft.VisualBasic.Strings.Asc(c);
27 int temp = 65536 + ascCode;
28 if (temp >= 45217 && temp <= 45252)
29 {
30 return "A";
31}
32 else if (temp >= 45253 && temp <= 45760)
33 {
34 return "B";
35}
36 else if (temp >= 45761 && temp <= 46317)
37 {
38 return "C";
39 }
40 else if (temp >= 46318 && temp <= 46825)
41 {
42 return "D";
43}
44
45
46 else if (temp >= 46826 && temp <= 47009)
47 {
48 return "E";
49 }
50 else if (temp >= 47010 && temp <= 47296)
51 {
52 return "F";
53}
54 else if (temp >= 47297 && temp <= 47613)
55 {
56 return "G";
57 }
58 else if (temp >= 47614 && temp <= 48118)
59 {
60 return "H";
61 }
62 else if (temp >= 48119 && temp <= 49061)
63 {
64 return "J";
65 }
66 else if (temp >= 49062 && temp <= 49323)
67 {
68 return "K";
69 }
70 else if (temp >= 49324 && temp <= 49895)
71 {
72 return "L";
73}
74 else if (temp >= 49896 && temp <= 50370)
75 {
76 return "M";
77 }
78 else if (temp >= 50371 && temp <= 50613)
79 {
80 return "N";
81 }
82 else if (temp >= 50614 && temp <= 50621)
83 {
84 return "O";
85}
86 else if (temp >= 50622 && temp <= 50905)
87 {
88 return "P";
89 }
90 else if (temp >= 50906 && temp <= 51386)
91 {
92 return "Q";
93}
94 else if (temp >= 51387 && temp <= 51445)
95 {
96 return "R";
97 }
98 else if (temp >= 51446 && temp <= 52217)
99 {
100 return "S";
101 }
102 else if (temp >= 52218 && temp <= 52697)
103 {
104 return "T";
105 }
106 else if (temp >= 52698 && temp <= 52979)
107 {
108 return "W";
109 }
110 else if (temp >= 52980 && temp <= 53688)
111 {
112 return "X";
113 }
114 else if (temp >= 53689 && temp <= 54480)
115 {
116 return "Y";
117 }
118 else if (temp >= 54481 && temp <= 62289)
119 {
120 return "Z";
121 }
122 else
123 {
124 return c.ToString();
125 }
126 }
127 }
128}
129
http://www.cnblogs.com/jillzhang/archive/2006/10/30/544596.html