汉字获取首字母
时间:2010-08-31 来源:valin
今天贴两段用通过c#实现汉字获取首字母的方法,希望对大家有所帮助。
方法一:
代码 class ChinesSpell{
private const int constA = 45217;
private const int constB = 45253;
private const int constC = 45761;
private const int constD = 46318;
private const int constE = 46826;
private const int constF = 47010;
private const int constG = 47297;
private const int constH = 47614;
private const int constJ = 48119;
private const int constK = 49062;
private const int constL = 49324;
private const int constM = 49896;
private const int constN = 50371;
private const int constO = 50614;
private const int constP = 50622;
private const int constQ = 50906;
private const int constR = 51387;
private const int constS = 51446;
private const int constT = 52218;
private const int constW = 52698;
private const int constX = 52980;
private const int constY = 53689;
private const int constZ = 54481;
public ChinesSpell()
{ }
public static string GetCharSpellCode(string CnChar)
{
long iCnChar;
byte[] HZ = System.Text.Encoding.Default.GetBytes(CnChar);
if (HZ.Length == 1)
{
return CnChar.ToUpper();
}
else
{
int a1 = (short)(HZ[0]);
int a2 = (short)(HZ[1]);
iCnChar = a1 * 256 + a2;
}
return GetChar(iCnChar);
}
private static string GetChar(long iCnChar)
{
if (CompareChar(iCnChar, constA, constB))
{
return "A";
}
if (CompareChar(iCnChar, constB, constC))
{
return "B";
}
if (CompareChar(iCnChar, constC, constD))
{
return "C";
}
if (CompareChar(iCnChar, constD, constE))
{
return "D";
}
if (CompareChar(iCnChar, constE, constF))
{
return "E";
}
if (CompareChar(iCnChar, constF, constG))
{
return "F";
}
if (CompareChar(iCnChar, constG, constH))
{
return "G";
}
if (CompareChar(iCnChar, constH, constJ))
{
return "H";
}
if (CompareChar(iCnChar, constJ, constK))
{
return "J";
}
if (CompareChar(iCnChar, constK, constL))
{
return "K";
}
if (CompareChar(iCnChar, constL, constM))
{
return "L";
}
if (CompareChar(iCnChar, constM, constN))
{
return "M";
}
if (CompareChar(iCnChar, constN, constO))
{
return "N";
}
if (CompareChar(iCnChar, constO, constP))
{
return "O";
}
if (CompareChar(iCnChar, constP, constQ))
{
return "P";
}
if (CompareChar(iCnChar, constQ, constR))
{
return "Q";
}
if (CompareChar(iCnChar, constR, constS))
{
return "R";
}
if (CompareChar(iCnChar, constS, constT))
{
return "S";
}
if (CompareChar(iCnChar, constT, constW))
{
return "T";
}
if (CompareChar(iCnChar, constW, constX))
{
return "W";
}
if (CompareChar(iCnChar, constX, constY))
{
return "X";
}
if (CompareChar(iCnChar, constY, constZ))
{
return "Y";
}
if (CompareChar(iCnChar, constZ, 55289))
{
return "Z";
}
else
{
return "*";
}
}
private static bool CompareChar(long iCnChar, int S, int E)
{
if (iCnChar >= S && iCnChar < E)
{
return true;
}
else
{
return false;
}
}
}
方法二:
代码 class ChinesSpell2
{
static public string GetChineseSpell(string strText)
{
int len = strText.Length;
string myStr = "";
for (int i = 0; i < len; i++)
{
myStr += getSpell(strText.Substring(i, 1));
}
return myStr;
}
static public string[] GetChineseSpell(string[] strText)
{
int len = strText.Length;
string[] myStr = null;
for (int i = 0; i < len; i++)
{
myStr[i] = getSpell(strText[i]);
}
return myStr;
}
static public string getSpell(string cnChar)
{
byte[] arrCN =System.Text.Encoding.Default.GetBytes(cnChar);
if (arrCN.Length > 1)
{
int area = (short)arrCN[0];
int pos = (short)arrCN[1];
int code = (area << 8) + pos;
int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
for (int i = 0; i < 26; i++)
{
int max = 55290;
if (i != 25) max = areacode[i + 1];
if (areacode[i] <= code && code < max)
{
return System.Text.Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
}
}
return "*";
}
else return cnChar;
}
}
相关阅读 更多 +