c#取汉字拼音首字母

 

代码

    
/// <summary>
    
/// GB2312的拼音算法,不支持多音字
    
/// </summary>
    public class GB2Alpha
    {
        
public static void GetAlpha(string a)
        {
            
int aa = Convert.ToInt32(a[0]);
            
byte[] arr = System.Text.Encoding.ASCII.GetBytes(a);
            
char c = convert(aa);
        }
        
//汉字首字母转换表
        private static char convert(int n)
        {
            
if (In(0xB0A10xB0C4, n)) return 'A';
            
if (In(0XB0C50XB2C0, n)) return 'B';
            
if (In(0xB2C10xB4ED, n)) return 'C';
            
if (In(0xB4EE0xB6E9, n)) return 'D';
            
if (In(0xB6EA0xB7A1, n)) return 'E';
            
if (In(0xB7A20xB8c0, n)) return 'F';
            
if (In(0xB8C10xB9FD, n)) return 'G';
            
if (In(0xB9FE0xBBF6, n)) return 'H';
            
if (In(0xBBF70xBFA5, n)) return 'J';
            
if (In(0xBFA60xC0AB, n)) return 'K';
            
if (In(0xC0AC0xC2E7, n)) return 'L';
            
if (In(0xC2E80xC4C2, n)) return 'M';
            
if (In(0xC4C30xC5B5, n)) return 'N';
            
if (In(0xC5B60xC5BD, n)) return 'O';
            
if (In(0xC5BE0xC6D9, n)) return 'P';
            
if (In(0xC6DA0xC8BA, n)) return 'Q';
            
if (In(0xC8BB0xC8F5, n)) return 'R';
            
if (In(0xC8F60xCBF0, n)) return 'S';
            
if (In(0xCBFA0xCDD9, n)) return 'T';
            
if (In(0xCDDA0xCEF3, n)) return 'W';
            
if (In(0xCEF40xD188, n)) return 'X';
            
if (In(0xD1B90xD4D0, n)) return 'Y';
            
if (In(0xD4D10xD7F9, n)) return 'Z';
            
return '\0';
        }
        
private static bool In(int start, int end, int code)
        {
            
if (code >= start && code <= end)
            {
                
return true;
            }
            
return false;
        }
    }

    
/// <summary>
    
/// 拼音首字母,支持多音字,
    
/// ,确保pinyin3.txt中汉字编码是连续的,汉字编码减去MinChar就得到在数组中的index,从而避免了作循环检测的消耗,所以全角字母的检测放在代码里面而没有放在pinyin3.txt中
    
/// </summary>
    public static class CSpell
    {
        
private static int MinChar;

        
private static int MaxChar;

        
/// <summary>
        
/// 确保pinyin3.txt中汉字编码是连续的,因为算法中要作减法运算
        
/// </summary>
        private static string[] Lines;
        
static bool init = false;
        
/// <summary>
        
/// 初始化
        
/// </summary>
        internal static void Init()
        {
            
if (init) return;
            init 
= true;
            Lines 
= Resource1.pinyin3.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            MinChar 
= (int)(Lines[0][0]);

            MaxChar 
= (int)(Lines[Lines.Length - 1][0]);
        }

        
/// <summary>
        
/// 检测,区分大小写,“浦发银行”=="YH"/"YX"
        
/// </summary>
        public static bool HitTestSpell(string strOrigin, string strCompare)
        {
            
string[][] arrStr = GetStringSpell(strOrigin);

            
if (strCompare.Length > strOrigin.Length) return false;
            
int index, beginIndex = 0;
            
for (int i = 0; i < strCompare.Length; i++)
            {
                index 
= Find(arrStr, strCompare[i].ToString(), beginIndex);
                
if (index < 0)
                {
                    
return false;
                }
                
else
                {
                    
//如果多次找到的拼音不连续,则失败
                    if (beginIndex != 0 && beginIndex != index)
                        
return false;
                    beginIndex 
= index + 1;
                }
            }

            
return true;
        }

        
/// <summary>
        
/// 检测,区分大小写,“浦发银行”=="YH"/"YX"
        
/// </summary>
        public static int HitTestSpellInt(string strOrigin, string strCompare)
        {
            
string[][] arrStr = GetStringSpell(strOrigin);

            
if (strCompare.Length > strOrigin.Length) return ErrCode.Fail;
            
int index, beginIndex = 0;
            
int firstIndex = -1;
            
for (int i = 0; i < strCompare.Length; i++)
            {
                index 
= Find(arrStr, strCompare[i].ToString(), beginIndex);
                
if (index < 0)
                {
                    
return ErrCode.Fail;
                }
                
else
                {
                    
if (firstIndex < 0) firstIndex = index;
                    beginIndex 
= index + 1;
                }
            }

            
return firstIndex;
        }

        
private static int Find(string[][] arrStr, string chr, int beginIndex)
        {
            
for (int i = beginIndex; i < arrStr.Length; i++)
            {
                
if (ContainsChar(arrStr[i], chr))
                {
                    
return i;
                }
            }
            
return -1;
        }

        
private static bool ContainsChar(string[] arrStr, string chr)
        {
            
for (int i = 0; i < arrStr.Length; i++)
            {
                
if (arrStr[i] == chr) return true;
            }
            
return false;
        }

        
/// <summary>
        
/// 返回字符串的拼音
        
/// </summary>
        public static string[][] GetStringSpell(string str)
        {
            
string[][] arrStr = new string[str.Length][];
            
for (int i = 0; i < str.Length; i++)
            {
                arrStr[i] 
= GetCharSpell(str[i]);
            }

            
return arrStr;
        }

        
/// <summary>
        
/// 返回字符的拼音
        
/// </summary>
        public static string[] GetCharSpell(char chr)
        {
            
int nchar = (int)chr;
            
if (nchar >= MinChar && nchar <= MaxChar)
            {
                
string[] splitChars = Lines[nchar - MinChar].Split(' ');
                
string[] result = new string[splitChars.Length - 1];
                
for (int i = 1; i < splitChars.Length; i++)
                {
                    result[i 
- 1= splitChars[i];
                }
                
return result;
            }
            
else
            {
                
//全角字母:A-Z
                if (chr >= char_A_QuanJiao && chr <= char_Z_QuanJiao)
                {
                    
string[] result = new string[1];

                    result[
0= ((char)(char_A + (chr - char_A_QuanJiao))).ToString();

                    
return result;
                }
                
else
                {
                    
//不是汉字则直接返回该string
                    string[] result = new string[1];
                    result[
0= chr.ToString();
                    
return result;
                }
            }
        }
        
private const char char_A_QuanJiao = '';
        
private const char char_Z_QuanJiao = '';
        
private const char char_A = 'A';
        
private const char char_Z = 'Z';
    }

 

 

 

pinyin3.txt  下载拼音表

posted @ 2010-07-01 12:35  风叙  阅读(267)  评论(0)    收藏  举报