您的位置:首页 > 其它

在.Net下获取中文字符拼音的首字母(一般排序时用到)

2009-03-24 16:18 429 查看
在.Net下获取中文字符拼音的首字母(一般排序时用到)
1



/**//*==============================================================================
2

* Name: ChineseLetter
3

* Author: Zhu JianFeng
4

* Date: 2007-5-7
5

* DESC: Get the first letter of pinyin according to specified Chinese character code
6

==============================================================================*/
7

using System;
8

using System.Collections.Generic;
9

using System.Text;
10


11

namespace Babyer
12





{
13

public class ChineseLetter
14





{
15

public static string GetFirstLetter(string chineseStr)
16





{
17

byte[] _cBs = System.Text.Encoding.Default.GetBytes(chineseStr);
18


19

if(_cBs.Length<2)
20

return chineseStr;
21


22

byte ucHigh, ucLow;
23

int nCode;
24


25

string strFirstLetter = string.Empty;
26


27

for (int i = 0; i < _cBs.Length; i++)
28





{
29


30

if (_cBs[i] < 0x80)
31





{
32

strFirstLetter += Encoding.Default.GetString(_cBs, i, 1);
33

continue;
34

}
35


36

ucHigh = (byte)_cBs[i];
37

ucLow = (byte)_cBs[i + 1];
38

if ( ucHigh < 0xa1 || ucLow < 0xa1)
39

continue;
40

else
41

// Treat code by section-position as an int type parameter,
42

// so make following change to nCode.
43

nCode = (ucHigh - 0xa0) * 100 + ucLow - 0xa0;
44


45

string str = FirstLetter(nCode);
46

strFirstLetter += str != string.Empty ? str : Encoding.Default.GetString(_cBs, i, 2);
47

i++;
48

}
49

return strFirstLetter;
50

}
51


52



/**//// <summary>
53

/// Get the first letter of pinyin according to specified Chinese character code
54

/// </summary>
55

/// <param name="nCode">the code of the chinese character</param>
56

/// <returns>receive the string of the first letter</returns>
57

public static string FirstLetter(int nCode)
58





{
59

string strLetter = string.Empty;
60


61

if(nCode >= 1601 && nCode < 1637) strLetter = "A";
62

if(nCode >= 1637 && nCode < 1833) strLetter = "B";
63

if(nCode >= 1833 && nCode < 2078) strLetter = "C";
64

if(nCode >= 2078 && nCode < 2274) strLetter = "D";
65

if(nCode >= 2274 && nCode < 2302) strLetter = "E";
66

if(nCode >= 2302 && nCode < 2433) strLetter = "F";
67

if(nCode >= 2433 && nCode < 2594) strLetter = "G";
68

if(nCode >= 2594 && nCode < 2787) strLetter = "H";
69

if(nCode >= 2787 && nCode < 3106) strLetter = "J";
70

if(nCode >= 3106 && nCode < 3212) strLetter = "K";
71

if(nCode >= 3212 && nCode < 3472) strLetter = "L";
72

if(nCode >= 3472 && nCode < 3635) strLetter = "M";
73

if(nCode >= 3635 && nCode < 3722) strLetter = "N";
74

if(nCode >= 3722 && nCode < 3730) strLetter = "O";
75

if(nCode >= 3730 && nCode < 3858) strLetter = "P";
76

if(nCode >= 3858 && nCode < 4027) strLetter = "Q";
77

if(nCode >= 4027 && nCode < 4086) strLetter = "R";
78

if(nCode >= 4086 && nCode < 4390) strLetter = "S";
79

if(nCode >= 4390 && nCode < 4558) strLetter = "T";
80

if(nCode >= 4558 && nCode < 4684) strLetter = "W";
81

if(nCode >= 4684 && nCode < 4925) strLetter = "X";
82

if(nCode >= 4925 && nCode < 5249) strLetter = "Y";
83

if(nCode >= 5249 && nCode < 5590) strLetter = "Z";
84


85

//if (strLetter == string.Empty)
86

// System.Windows.Forms.MessageBox.Show(String.Format("信息:/n{0}为非常用字符编码,不能为您解析相应匹配首字母!",nCode));
87

return strLetter;
88

}
89

}
90

}

这里只能解析常用字符,如果非常用字符,我想只有用字典了,如果有更好的方法也欢迎一起探讨.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: