#region 改写VB的方法取汉字首字母
/// <summary>
/// 从一个字符串中提取汉字首字母,如果是中间有别的字符,直接取回该字符
/// </summary>
/// <param name="chinese">要提取的字符串</param>
/// <returns></returns>
public static string GetPhonics(string chinese)
{
int len = chinese.Length;
char[] charArray = new char[len];
//byte[] byteArray = new byte[2];
for (int i = 0; i < len; i++)
{
charArray[i] = GetLetter(chinese[i]);
//byteArray = System.Text.Encoding.Default.GetBytes(chinese[i].ToString());
}
string txt = new string(charArray);
return txt;
}
/// <summary>
/// 取一个汉字的首字母
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
private static char GetLetter(char c)
{
int codeNum;
codeNum = 65536 + GetAscii(c);
if (codeNum >= 45217 && codeNum <= 45252) { return 'A'; }
else if (codeNum >= 45253 && codeNum <= 45760) { return 'B'; }
else if (codeNum >= 45761 && codeNum <= 46317) { return 'C'; }
else if (codeNum >= 46318 && codeNum <= 46825) { return 'D'; }
else if (codeNum >= 46826 && codeNum <= 47009) { return 'E'; }
else if (codeNum >= 47010 && codeNum <= 47296) { return 'F'; }
else if (codeNum >= 47297 && codeNum <= 47613) { return 'G'; }
else if (codeNum >= 47614 && codeNum <= 48118) { return 'H'; }
else if (codeNum >= 48119 && codeNum <= 49061) { return 'J'; }
else if (codeNum >= 49062 && codeNum <= 49323) { return 'K'; }
else if (codeNum >= 49324 && codeNum <= 49895) { return 'L'; }
else if (codeNum >= 49896 && codeNum <= 50370) { return 'M'; }
else if (codeNum >= 50371 && codeNum <= 50613) { return 'N'; }
else if (codeNum >= 50614 && codeNum <= 50621) { return 'O'; }
else if (codeNum >= 50622 && codeNum <= 50905) { return 'P'; }
else if (codeNum >= 50906 && codeNum <= 51386) { return 'Q'; }
else if (codeNum >= 51387 && codeNum <= 51445) { return 'R'; }
else if (codeNum >= 51446 && codeNum <= 52217) { return 'S'; }
else if (codeNum >= 52218 && codeNum <= 52697) { return 'T'; }
else if (codeNum >= 52698 && codeNum <= 52979) { return 'W'; }
else if (codeNum >= 52980 && codeNum <= 53688) { return 'X'; }
else if (codeNum >= 53689 && codeNum <= 54480) { return 'Y'; }
else if (codeNum >= 54481 && codeNum <= 62289) { return 'Z'; }
else return c;
}
/// <summary>
/// 取一个字符的ASCII码
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
private static int GetAscii(char c)
{
Encoding ecode = Encoding.GetEncoding("gb18030");
byte[] codebytes = ecode.GetBytes(c.ToString());
if (IsTwoBytesChar(c))
{
//双字节码为高位乘256,再加低位
//该为无符号码,再减65536
return (int)codebytes[0] * 256 + (int)codebytes[1] - 65536;
}
else
{
return (int)codebytes[0];
}
}
/// <summary>
/// 是否为双字节字符
/// </summary>
public static bool IsTwoBytesChar(char c)
{
string str = c.ToString();
//使用中文支持编码
Encoding ecode = Encoding.GetEncoding("gb18030");
if (ecode.GetByteCount(str) == 2)
{
return true;
}
else
{
return false;
}
}
#endregion
#region 获取一串汉字的拼音声母
/// <summary>
/// 获取一串汉字的拼音声母
/// </summary>
/// <param name="chinese">Unicode格式的汉字字符串</param>
/// <returns>拼音声母字符串</returns>
public String ConvertLetter(String chinese)
{
char[] buffer = new char[chinese.Length];
for (int i = 0; i < chinese.Length; i++)
{
buffer[i] = ConvertChar(chinese[i]);
}
return new String(buffer);
}
/// <summary>
/// 获取一个汉字的拼音声母
/// </summary>
/// <param name="chinese">Unicode格式的一个汉字</param>
/// <returns>汉字的声母</returns>
public char ConvertChar(Char chinese)
{
Encoding gb2312 = Encoding.GetEncoding("GB2312");
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(new Char[] { chinese });
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, gb2312, unicodeBytes);
// 计算该汉字的GB-2312编码
int n = (int)asciiBytes[0] << 8;
n += (int)asciiBytes[1];
// 根据汉字区域码获取拼音声母
if (In(0xB0A1, 0xB0C4, n)) return 'A';
if (In(0XB0C5, 0XB2C0, n)) return 'B';
if (In(0xB2C1, 0xB4ED, n)) return 'C';
if (In(0xB4EE, 0xB6E9, n)) return 'D';
if (In(0xB6EA, 0xB7A1, n)) return 'E';
if (In(0xB7A2, 0xB8c0, n)) return 'F';
if (In(0xB8C1, 0xB9FD, n)) return 'G';
if (In(0xB9FE, 0xBBF6, n)) return 'H';
if (In(0xBBF7, 0xBFA5, n)) return 'J';
if (In(0xBFA6, 0xC0AB, n)) return 'K';
if (In(0xC0AC, 0xC2E7, n)) return 'L';
if (In(0xC2E8, 0xC4C2, n)) return 'M';
if (In(0xC4C3, 0xC5B5, n)) return 'N';
if (In(0xC5B6, 0xC5BD, n)) return 'O';
if (In(0xC5BE, 0xC6D9, n)) return 'P';
if (In(0xC6DA, 0xC8BA, n)) return 'Q';
if (In(0xC8BB, 0xC8F5, n)) return 'R';
if (In(0xC8F6, 0xCBF0, n)) return 'S';
if (In(0xCBFA, 0xCDD9, n)) return 'T';
if (In(0xCDDA, 0xCEF3, n)) return 'W';
if (In(0xCEF4, 0xD188, n)) return 'X';
if (In(0xD1B9, 0xD4D0, n)) return 'Y';
if (In(0xD4D1, 0xD7F9, n)) return 'Z';
return ' ';
}
private bool In(int Lp, int Hp, int Value)
{
return ((Value <= Hp) && (Value >= Lp));
}
#endregion