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

本文介绍了一个.NET程序,用于从指定的中文字符中提取其拼音的首字母。该程序适用于常用汉字,并通过分析字符编码来实现转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1ExpandedBlockStart.gifContractedBlock.gif/**//*==============================================================================
 2InBlock.gif * Name: ChineseLetter
 3InBlock.gif * Author: Zhu JianFeng
 4InBlock.gif * Date: 2007-5-7
 5InBlock.gif * DESC: Get the first letter of pinyin according to specified Chinese character code
 6ExpandedBlockEnd.gif ==============================================================================*/

 7None.gifusing System;
 8None.gifusing System.Collections.Generic;
 9None.gifusing System.Text;
10None.gif
11None.gifnamespace Babyer
12ExpandedBlockStart.gifContractedBlock.gifdot.gif{
13InBlock.gif    public class ChineseLetter
14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
15InBlock.gif       public static string GetFirstLetter(string chineseStr)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            byte[] _cBs = System.Text.Encoding.Default.GetBytes(chineseStr);
18InBlock.gif
19InBlock.gif            if(_cBs.Length<2)
20InBlock.gif                return chineseStr;
21InBlock.gif
22InBlock.gif            byte ucHigh, ucLow;
23InBlock.gif            int  nCode;
24InBlock.gif
25InBlock.gif            string strFirstLetter = string.Empty;
26InBlock.gif
27InBlock.gif            for (int i = 0; i < _cBs.Length; i++)
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif
30InBlock.gif                if (_cBs[i] < 0x80)
31ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
32InBlock.gif                    strFirstLetter += Encoding.Default.GetString(_cBs, i, 1);
33InBlock.gif                    continue;
34ExpandedSubBlockEnd.gif                }

35InBlock.gif
36InBlock.gif                ucHigh = (byte)_cBs[i];
37InBlock.gif                ucLow = (byte)_cBs[i + 1];
38InBlock.gif                if ( ucHigh < 0xa1 || ucLow < 0xa1)
39InBlock.gif                    continue;
40InBlock.gif                else
41InBlock.gif                // Treat code by section-position as an int type parameter,
42InBlock.gif                // so make following change to nCode.
43InBlock.gif                    nCode = (ucHigh - 0xa0* 100 + ucLow - 0xa0;
44InBlock.gif
45InBlock.gif                string str = FirstLetter(nCode);
46InBlock.gif                strFirstLetter += str != string.Empty ? str : Encoding.Default.GetString(_cBs, i, 2);
47InBlock.gif                i++;
48ExpandedSubBlockEnd.gif            }

49InBlock.gif            return strFirstLetter;
50ExpandedSubBlockEnd.gif        }

51InBlock.gif
52ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
53InBlock.gif        /// Get the first letter of pinyin according to specified Chinese character code
54InBlock.gif        /// </summary>
55InBlock.gif        /// <param name="nCode">the code of the chinese character</param>
56ExpandedSubBlockEnd.gif        /// <returns>receive the string of the first letter</returns>

57InBlock.gif        public static string FirstLetter(int nCode)
58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
59InBlock.gif            string strLetter = string.Empty;
60InBlock.gif
61InBlock.gif            if(nCode >= 1601 && nCode < 1637) strLetter = "A";
62InBlock.gif            if(nCode >= 1637 && nCode < 1833) strLetter = "B";
63InBlock.gif            if(nCode >= 1833 && nCode < 2078) strLetter = "C";
64InBlock.gif            if(nCode >= 2078 && nCode < 2274) strLetter = "D";
65InBlock.gif            if(nCode >= 2274 && nCode < 2302) strLetter = "E";
66InBlock.gif            if(nCode >= 2302 && nCode < 2433) strLetter = "F";
67InBlock.gif            if(nCode >= 2433 && nCode < 2594) strLetter = "G";
68InBlock.gif            if(nCode >= 2594 && nCode < 2787) strLetter = "H";
69InBlock.gif            if(nCode >= 2787 && nCode < 3106) strLetter = "J";
70InBlock.gif            if(nCode >= 3106 && nCode < 3212) strLetter = "K";
71InBlock.gif            if(nCode >= 3212 && nCode < 3472) strLetter = "L";
72InBlock.gif            if(nCode >= 3472 && nCode < 3635) strLetter = "M";
73InBlock.gif            if(nCode >= 3635 && nCode < 3722) strLetter = "N";
74InBlock.gif            if(nCode >= 3722 && nCode < 3730) strLetter = "O";
75InBlock.gif            if(nCode >= 3730 && nCode < 3858) strLetter = "P";
76InBlock.gif            if(nCode >= 3858 && nCode < 4027) strLetter = "Q";
77InBlock.gif            if(nCode >= 4027 && nCode < 4086) strLetter = "R";
78InBlock.gif            if(nCode >= 4086 && nCode < 4390) strLetter = "S";
79InBlock.gif            if(nCode >= 4390 && nCode < 4558) strLetter = "T";
80InBlock.gif            if(nCode >= 4558 && nCode < 4684) strLetter = "W";
81InBlock.gif            if(nCode >= 4684 && nCode < 4925) strLetter = "X";
82InBlock.gif            if(nCode >= 4925 && nCode < 5249) strLetter = "Y";
83InBlock.gif            if(nCode >= 5249 && nCode < 5590) strLetter = "Z";
84InBlock.gif
85InBlock.gif            //if (strLetter == string.Empty)
86InBlock.gif            //    System.Windows.Forms.MessageBox.Show(String.Format("信息:\n{0}为非常用字符编码,不能为您解析相应匹配首字母!",nCode));
87InBlock.gif            return strLetter;
88ExpandedSubBlockEnd.gif       }

89ExpandedSubBlockEnd.gif    }

90ExpandedBlockEnd.gif}
这里只能解析常用字符,如果非常用字符,我想只有用字典了,如果有更好的方法也欢迎一起探讨.

转载于:https://www.cnblogs.com/apexchu/archive/2007/05/09/740712.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值