程序中经常需要用到汉字的首字母来进行检索文字,首先需要转换成首字母
方法一:
/// <summary>
/// 取字符串首字母(包括中文)
/// </summary>
/// <param name="name">字符串</param>
/// <returns>首字母</returns>
public static string FirstLetter(string name)
{
if (string.IsNullOrEmpty(name)) return "";
var s = name.Substring(0, 1);
if ((int)Convert.ToChar(s) > 0 && (int)Convert.ToChar(s) < 256) return "";
byte[] array = System.Text.Encoding.Default.GetBytes(s);
if (array == null || array.Length < 2) return "*";
int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
if (i < 0xB0A1) return "*";
if (i < 0xB0C5) return "A";
if (i < 0xB2C1) return "B";
if (i < 0xB4EE) return "C";
if (i < 0xB6EA) return "D";
if (i < 0xB7A2) return "E";
if (i < 0xB8C1) return "F";
if (i < 0xB9FE) return "G";
if (i < 0xBBF7) return "H";
if (i < 0xBFA6) return "J";
if (i < 0xC0AC) return "K";
if (i < 0xC2E8) return "L";
if (i < 0xC4C3) return "M";
if (i < 0xC5B6) return "N";
if (i < 0xC5BE) return "O";
if (i < 0xC6DA) return "P";
if (i < 0xC8BB) return "Q";
if (i < 0xC8F6) return "R";
if (i < 0xCBFA) return "S";
if (i < 0xCDDA) return "T";
if (i < 0xCEF4) return "W";
if (i < 0xD1B9) return "X";
if (i < 0xD4D1) return "Y";
if (i < 0xD7FA) return "Z";
return "*";
}
方法二:
/// <summary>
/// 得到单个字符的首字母方法2
/// </summary>
/// <param name="name">字符串</param>
/// <returns></returns>
public static string FirstLetterTwo(string name)
{
if (string.IsNullOrEmpty(name)) return "";
name = name.Substring(0, 1);
if (Convert.ToChar(name) >= 0 && Convert.ToChar(name) < 256) { return name; }
else
{
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] unicodeBytes = Encoding.Unicode.GetBytes(name);
byte[] gb2312Bytes = Encoding.Convert(Encoding.Unicode, gb2312, unicodeBytes);
return FirstLetterX(Convert.ToInt32(string.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[0]) - 160) + string.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[1]) - 160)));
}
}
//根据区位得到首字母
private static string FirstLetterX(int name)
{
if (name >= 1601 && name < 1637) return "A";
if (name >= 1637 && name < 1833) return "B";
if (name >= 1833 && name < 2078) return "C";
if (name >= 2078 && name < 2274) return "D";
if (name >= 2274 && name < 2302) return "E";
if (name >= 2302 && name < 2433) return "F";
if (name >= 2433 && name < 2594) return "G";
if (name >= 2594 && name < 2787) return "H";
if (name >= 2787 && name < 3106) return "J";
if (name >= 3106 && name < 3212) return "K";
if (name >= 3212 && name < 3472) return "L";
if (name >= 3472 && name < 3635) return "M";
if (name >= 3635 && name < 3722) return "N";
if (name >= 3722 && name < 3730) return "O";
if (name >= 3730 && name < 3858) return "P";
if (name >= 3858 && name < 4027) return "Q";
if (name >= 4027 && name < 4086) return "R";
if (name >= 4086 && name < 4390) return "S";
if (name >= 4390 && name < 4558) return "T";
if (name >= 4558 && name < 4684) return "W";
if (name >= 4684 && name < 4925) return "X";
if (name >= 4925 && name < 5249) return "Y";
if (name >= 5249 && name <= 5589) return "Z";
if (name >= 5601 && name <= 8794)
{
string CodeData = "cjwgnspgcenegypbtwxzdxykygtpjnmjqmbsgzscyjsyyfpggbzgydywjkgaljswkbjqhyjwpdzlsgmr"
+ "ybywwccgznkydgttngjeyekzydcjnmcylqlypyqbqrpzslwbdgkjfyxjwcltbncxjjjjcxdtqsqzycdxxhgckbphffss"
+ "pybgmxjbbyglbhlssmzmpjhsojnghdzcdklgjhsgqzhxqgkezzwymcscjnyetxadzpmdssmzjjqjyzcjjfwqjbdzbjgd"
+ "nzcbwhgxhqkmwfbpbqdtjjzkqhylcgxfptyjyyzpsjlfchmqshgmmxsxjpkdcmbbqbefsjwhwwgckpylqbgldlcctnma"
+ "eddksjngkcsgxlhzaybdbtsdkdylhgymylcxpycjndqjwxqxfyyfjlejbzrwccqhqcsbzkymgplbmcrqcflnymyqmsqt"
+ "rbcjthztqfrxchxmcjcjlxqgjmshzkbswxemdlckfsydsglycjjssjnqbjctyhbftdcyjdgwyghqfrxwckqkxebpdjpx"
+ "jqsrmebwgjlbjslyysmdxlclqkxlhtjrjjmbjhxhwywcbhtrxxglhjhfbmgykldyxzpplggpmtcbbajjzyljtyanjgbj"
+ "flqgdzyqcaxbkclecjsznslyzhlxlzcghbxzhznytdsbcjkdlzayffydlabbgqszkggldndnyskjshdlxxbcghxyggdj"
+ "mmzngmmccgwzszxsjbznmlzdthcqydbdllscddnlkjyhjsycjlkohqasdhnhcsgaehdaashtcplcpqybsdmpjlpcjaql"
+ "cdhjjasprchngjnlhlyyqyhwzpnccgwwmzffjqqqqxxaclbhkdjxdgmmydjxzllsygxgkjrywzwyclzmcsjzldbndcfc"
+ "xyhlschycjqppqagmnyxpfrkssbjlyxyjjglnscmhcwwmnzjjlhmhchsyppttxrycsxbyhcsmxjsxnbwgpxxtaybgajc"
+ "xlypdccwqocwkccsbnhcpdyznbcyytyckskybsqkkytqqxfcwchcwkelcqbsqyjqcclmthsywhmktlkjlychwheqjhtj"
+ "hppqpqscfymmcmgbmhglgsllysdllljpchmjhwljcyhzjxhdxjlhxrswlwzjcbxmhzqxsdzpmgfcsglsdymjshxpjxom"
+ "yqknmyblrthbcftpmgyxlchlhlzylxgsssscclsldclepbhshxyyfhbmgdfycnjqwlqhjjcywjztejjdhfblqxtqkwhd"
+ "chqxagtlxljxmsljhdzkzjecxjcjnmbbjcsfywkbjzghysdcpqyrsljpclpwxsdwejbjcbcnaytmgmbapclyqbclzxcb"
+ "nmsggfnzjjbzsfqyndxhpcqkzczwalsbccjxpozgwkybsgxfcfcdkhjbstlqfsgdslqwzkxtmhsbgzhjcrglyjbpmljs"
+ "xlcjqqhzmjczydjwbmjklddpmjegxyhylxhlqyqhkycwcjmyhxnatjhyccxzpcqlbzwwwtwbqcmlbmynjcccxbbsnzzl"
+ "jpljxyztzlgcldcklyrzzgqtgjhhgjljaxfgfjzslcfdqzlclgjdjcsnclljpjqdcclcjxmyzftsxgcgsbrzxjqqcczh"
+ "gyjdjqqlzxjyldlbcyamcstylbdjbyregklzdzhldszchznwczcllwjqjjjkdgjcolbbzppglghtgzcygezmycnqcycy"
+ "hbhgxkamtxyxnbskyzzgjzlqjdfcjxdygjqjjpmgwgjjjpkjsbgbmmcjssclpqpdxcdyykypcjddyygywchjrtgcnyql"
+ "dkljczzgzccjgdyksgpzmdlcphnjafyzdjcnmwescsglbtzcgmsdllyxqsxsbljsbbsgghfjlwpmzjnlyywdqshzxtyy"
+ "whmcyhywdbxbtlmswyyfsbjcbdxxlhjhfpsxzqhfzmqcztqcxzxrdkdjhnnyzqqfnqdmmgnydxmjgdhcdycbffallztd"
+ "ltfkmxqzdngeqdbdczjdxbzgsqqddjcmbkxffxmkdmcsychzcmljdjynhprsjmkmpcklgdbqtfzswtfgglyplljzhgjj"
+ "gypzltcsmcnbtjbhfkdhbyzgkpbbymtdlsxsbnpdkleycjnycdykzddhqgsdzsctarlltkzlgecllkjljjaqnbdggghf"
+ "jtzqjsecshalqfmmgjnlyjbbtmlycxdcjpldlpcqdhsycbzsckbzmsljflhrbjsnbrgjhxpdgdjybzgdlgcsezgxlblg"
+ "yxtwmabchecmwyjyzlljjshlgndjlslygkdzpzxjyyzlpcxszfgwyydlyhcljscmbjhblyjlycblydpdqysxktbytdkd"
+ "xjypcnrjmfdjgklccjbctbjddbblblcdqrppxjcglzcshltoljnmdddlngkaqakgjgyhheznmshrphqqjchgmfprxcjg"
+ "dychghlyrzqlcngjnzsqdkqjymszswlcfqjqxgbggxmdjwlmcrnfkkfsyyljbmqammmycctbshcptxxzzsmphfshmclm"
+ "ldjfyqxsdyjdjjzzhqpdszglssjbckbxyqzjsgpsxjzqznqtbdkwxjkhhgflbcsmdldgdzdblzkycqnncsybzbfglzzx"
+ "swmsccmqnjqsbdqsjtxxmbldxcclzshzcxrqjgjylxzfjphymzqqydfqjjlcznzjcdgzygcdxmzysctlkphtxhtlbjxj"
+ "lxscdqccbbqjfqzfsltjbtkqbsxjjljchczdbzjdczjccprnlqcgpfczlclcxzdmxmphgsgzgszzqjxlwtjpfsyaslcj"
+ "btckwcwmytcsjjljcqlwzmalbxyfbpnlschtgjwejjxxglljstgshjqlzfkcgnndszfdeqfhbsaqdgylbxmmygszldyd"
+ "jmjjrgbjgkgdhgkblgkbdmbylxwcxyttybkmrjjzxqjbhlmhmjjzmqasldcyxyqdlqcafywyxqhz";
string _gbcode = name.ToString();
int pos = (Convert.ToInt16(_gbcode.Substring(0, 2)) - 56) * 94 + Convert.ToInt16(_gbcode.Substring(_gbcode.Length - 2, 2));
return CodeData.Substring(pos - 1, 1);
}
return "0";
}
方法三:
/// <summary>
/// 得到单个字符的首字母方法3
/// </summary>
/// <param name="cnChar"></param>
/// <returns></returns>
public static string FirstLetterThere(string cnChar)
{
byte[] arrCN = 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 Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
}
}
return "*";
}
else return cnChar;
}