From: http://blog.csdn.net/qqiuzaihui/archive/2009/01/06/3721590.aspx
Add namespace
Using system. Text. regularexpressions;
/// <Summary>
/// Determine whether it is a Chinese character
/// </Summary>
/// <Param name = "chrstr"> string to be detected </param>
/// <Returns> returns true for Chinese characters </returns>
Public bool ischinesecharacters (string chrstr)
{
RegEx checkstr = new RegEx ("[\ u4e00-\ u9fa5]");
Return checkstr. ismatch (chrstr );
}
/// <Summary>
/// Obtain the first letter and letter (uppercase) of each Chinese Character)
/// </Summary>
/// <Param name = "chrstr"> input string </param>
/// <Returns> return result </returns>
Public String getheadcharacter (string chrstr)
{
String strheadstring = string. empty;
Encoding GB = system. Text. encoding. getencoding ("gb2312 ");
For (INT I = 0; I <chrstr. length; I ++)
{
// Check whether the character is a Chinese character
If (! Ischinesecharacters (chrstr. substring (I, 1 )))
{
Strheadstring + = chrstr. substring (I, 1 );
Continue;
}
Byte [] bytes = GB. getbytes (chrstr. substring (I, 1 ));
String lowcode = system. Convert. tostring (Bytes [0]-0xa0, 16 );
String hightcode = system. Convert. tostring (Bytes [1]-0xa0, 16 );
Int ncode = convert. touint16 (lowcode, 16) * 100 + convert. touint16 (hightcode, 16); // get the location code
Strheadstring + = firstletter (ncode );
}
Return strheadstring;
}
/// <Summary>
/// Obtain the first letter (uppercase) of a Chinese character location code)
/// </Summary>
/// <Param name = "ncode"> Chinese character encoding </param>
/// <Returns> </returns>
Public String firstletter (INT ncode)
{
If (ncode> = 1601 & ncode <1637) Return "";
If (ncode> = 1637 & ncode <1833) Return "B ";
If (ncode> = 1833 & ncode <2078) Return "C ";
If (ncode> = 2078 & ncode <2274) Return "D ";
If (ncode> = 2274 & ncode <2302) Return "E ";
If (ncode> = 2302 & ncode <2433) Return "F ";
If (ncode> = 2433 & ncode <2594) Return "G ";
If (ncode> = 2594 & ncode <2787) Return "H ";
If (ncode> = 2787 & ncode <3106) Return "J ";
If (ncode> = 3106 & ncode <3212) Return "K ";
If (ncode> = 3212 & ncode <3472) Return "L ";
If (ncode> = 3472 & ncode <3635) Return "M ";
If (ncode> = 3635 & ncode <3722) Return "N ";
If (ncode> = 3722 & ncode <3730) Return "O ";
If (ncode> = 3730 & ncode <3858) Return "p ";
If (ncode> = 3858 & ncode <4027) Return "Q ";
If (ncode> = 4027 & ncode <4086) Return "R ";
If (ncode> = 4086 & ncode <4390) Return "S ";
If (ncode> = 4390 & ncode <4558) Return "T ";
If (ncode> = 4558 & ncode <4684) Return "W ";
If (ncode> = 4684 & ncode <4925) Return "X ";
If (ncode> = 4925 & ncode <5249) Return "Y ";
If (ncode> = 5249 & ncode <5590) Return "Z ";
Return "";
}
}