获取字符串中的汉字拼音首字母
程序员文章站
2023-10-30 15:39:58
获取字符串中的汉字拼音首字母(根据http://blog.csdn.net/iwebsms/archive/2004/09/28/119851.aspx修改) 可能代码比较...
获取字符串中的汉字拼音首字母(根据http://blog.csdn.net/iwebsms/archive/2004/09/28/119851.aspx修改)
可能代码比较臃肿,希望有朋友再帮忙改正
//返回给定字符串的首字母
private string indexcode(string indextxt)
{
string _temp=null;
for(int i=0;i<indextxt.length;i++)
_temp = _temp + getoneindex( indextxt.substring(i,1) );
return _temp;
}
//得到单个字符的首字母
private string getoneindex(string oneindextxt)
{
if(convert.tochar(oneindextxt)>=0 && convert.tochar(oneindextxt)<256)
return oneindextxt;
else
{
encoding gb2312 = encoding.getencoding("gb2312");
byte[] unicodebytes = encoding.unicode.getbytes(oneindextxt);
byte[] gb2312bytes = encoding.convert(encoding.unicode, gb2312, unicodebytes);
return getx(convert.toint32(
string.format("{0:d2}",convert.toint16(gb2312bytes[0])-160)
+ string.format("{0:d2}",convert.toint16(gb2312bytes[1])-160)
));
}
}
//根据区位得到首字母
private string getx(int gbcode)
{
if(gbcode >= 1601 && gbcode < 1637)return "a";
if(gbcode >= 1637 && gbcode < 1833)return "b";
if(gbcode >= 1833 && gbcode < 2078)return "c";
if(gbcode >= 2078 && gbcode < 2274)return "d";
if(gbcode >= 2274 && gbcode < 2302)return "e";
if(gbcode >= 2302 && gbcode < 2433)return "f";
if(gbcode >= 2433 && gbcode < 2594)return "g";
if(gbcode >= 2594 && gbcode < 2787)return "h";
if(gbcode >= 2787 && gbcode < 3106)return "j";
if(gbcode >= 3106 && gbcode < 3212)return "k";
if(gbcode >= 3212 && gbcode < 3472)return "l";
if(gbcode >= 3472 && gbcode < 3635)return "m";
if(gbcode >= 3635 && gbcode < 3722)return "n";
if(gbcode >= 3722 && gbcode < 3730)return "o";
if(gbcode >= 3730 && gbcode < 3858)return "p";
if(gbcode >= 3858 && gbcode < 4027)return "q";
if(gbcode >= 4027 && gbcode < 4086)return "r";
if(gbcode >= 4086 && gbcode < 4390)return "s";
if(gbcode >= 4390 && gbcode < 4558)return "t";
if(gbcode >= 4558 && gbcode < 4684)return "w";
if(gbcode >= 4684 && gbcode < 4925)return "x";
if(gbcode >= 4925 && gbcode < 5249)return "y";
if(gbcode >= 5249 && gbcode <= 5589)return "z";
if(gbcode >= 5601 && gbcode <= 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 = gbcode.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 " ";
}
可能代码比较臃肿,希望有朋友再帮忙改正
//返回给定字符串的首字母
private string indexcode(string indextxt)
{
string _temp=null;
for(int i=0;i<indextxt.length;i++)
_temp = _temp + getoneindex( indextxt.substring(i,1) );
return _temp;
}
//得到单个字符的首字母
private string getoneindex(string oneindextxt)
{
if(convert.tochar(oneindextxt)>=0 && convert.tochar(oneindextxt)<256)
return oneindextxt;
else
{
encoding gb2312 = encoding.getencoding("gb2312");
byte[] unicodebytes = encoding.unicode.getbytes(oneindextxt);
byte[] gb2312bytes = encoding.convert(encoding.unicode, gb2312, unicodebytes);
return getx(convert.toint32(
string.format("{0:d2}",convert.toint16(gb2312bytes[0])-160)
+ string.format("{0:d2}",convert.toint16(gb2312bytes[1])-160)
));
}
}
//根据区位得到首字母
private string getx(int gbcode)
{
if(gbcode >= 1601 && gbcode < 1637)return "a";
if(gbcode >= 1637 && gbcode < 1833)return "b";
if(gbcode >= 1833 && gbcode < 2078)return "c";
if(gbcode >= 2078 && gbcode < 2274)return "d";
if(gbcode >= 2274 && gbcode < 2302)return "e";
if(gbcode >= 2302 && gbcode < 2433)return "f";
if(gbcode >= 2433 && gbcode < 2594)return "g";
if(gbcode >= 2594 && gbcode < 2787)return "h";
if(gbcode >= 2787 && gbcode < 3106)return "j";
if(gbcode >= 3106 && gbcode < 3212)return "k";
if(gbcode >= 3212 && gbcode < 3472)return "l";
if(gbcode >= 3472 && gbcode < 3635)return "m";
if(gbcode >= 3635 && gbcode < 3722)return "n";
if(gbcode >= 3722 && gbcode < 3730)return "o";
if(gbcode >= 3730 && gbcode < 3858)return "p";
if(gbcode >= 3858 && gbcode < 4027)return "q";
if(gbcode >= 4027 && gbcode < 4086)return "r";
if(gbcode >= 4086 && gbcode < 4390)return "s";
if(gbcode >= 4390 && gbcode < 4558)return "t";
if(gbcode >= 4558 && gbcode < 4684)return "w";
if(gbcode >= 4684 && gbcode < 4925)return "x";
if(gbcode >= 4925 && gbcode < 5249)return "y";
if(gbcode >= 5249 && gbcode <= 5589)return "z";
if(gbcode >= 5601 && gbcode <= 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 = gbcode.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 " ";
}
上一篇: 科沃斯开启智能机器人新时代
下一篇: 变量、函数、类的命名规则