• Asp.net 将中文汉字转换成拼音首字和拼音全拼


    1. /// <summary>  
    2.     /// 获取汉字的首字母和全拼  
    3.     /// </summary>  
    4.     public class ChineseCode  
    5.     {  
    6.         protected string _CnTxt;  
    7.         protected string _EnTxt;  
    8.   
    9.         /// <summary>  
    10.         /// 要获取字母的汉字  
    11.         /// </summary>  
    12.         public string CnTxt  
    13.         {  
    14.             get { return _CnTxt; }  
    15.             set { _CnTxt = value; }  
    16.         }  
    17.         /// <summary>  
    18.         /// 汉字的首字母  
    19.         /// </summary>  
    20.         public string EnTxt  
    21.         {  
    22.             get { return _EnTxt; }  
    23.             set { _EnTxt = value; }  
    24.         }  
    25.         /// <summary>  
    26.         /// 构造方法  
    27.         /// </summary>  
    28.         public ChineseCode()  
    29.         {  
    30.   
    31.         }  
    32.         /// <summary>  
    33.         /// 构造方法,获取汉字首字母拼音  
    34.         /// </summary>  
    35.         /// <param name="txt"></param>  
    36.         public ChineseCode(string txt)  
    37.         {  
    38.             _CnTxt = txt;  
    39.             _EnTxt = IndexCode(_CnTxt);  
    40.         }  
    41.         /// <summary>  
    42.         /// 获取汉字的首字母  
    43.         /// </summary>  
    44.         /// <param name="IndexTxt">汉字</param>  
    45.         /// <returns>汉字的首字母</returns>  
    46.         public String IndexCode(String IndexTxt)  
    47.         {  
    48.             String _Temp = null;  
    49.             for (int i = 0; i < IndexTxt.Length; i++)  
    50.                 _Temp = _Temp + GetOneIndex(IndexTxt.Substring(i, 1));  
    51.             return _Temp;  
    52.         }  
    53.   
    54.         /// <summary>  
    55.         /// 单个汉字  
    56.         /// </summary>  
    57.         /// <param name="OneIndexTxt">汉字</param>  
    58.         /// <returns>首拼</returns>  
    59.         private String GetOneIndex(String OneIndexTxt)  
    60.         {  
    61.             if (Convert.ToChar(OneIndexTxt) >= 0 && Convert.ToChar(OneIndexTxt) < 256)  
    62.                 return OneIndexTxt;  
    63.             else  
    64.             {  
    65.                 Encoding gb2312 = Encoding.GetEncoding("gb2312");  
    66.                 byte[] unicodeBytes = Encoding.Unicode.GetBytes(OneIndexTxt);  
    67.                 byte[] gb2312Bytes = Encoding.Convert(Encoding.Unicode, gb2312, unicodeBytes);  
    68.                 return GetX(Convert.ToInt32(  
    69.                 String.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[0]) - 160)  
    70.                 + String.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[1]) - 160)  
    71.                 ));  
    72.             }  
    73.         }  
    74.         /// <summary>  
    75.         /// 根据区位得到首字母  
    76.         /// </summary>  
    77.         /// <param name="GBCode">区位</param>  
    78.         /// <returns></returns>  
    79.         private String GetX(int GBCode)  
    80.         {  
    81.             if (GBCode >= 1601 && GBCode < 1637) return "a";  
    82.             if (GBCode >= 1637 && GBCode < 1833) return "b";  
    83.             if (GBCode >= 1833 && GBCode < 2078) return "c";  
    84.             if (GBCode >= 2078 && GBCode < 2274) return "d";  
    85.             if (GBCode >= 2274 && GBCode < 2302) return "e";  
    86.             if (GBCode >= 2302 && GBCode < 2433) return "f";  
    87.             if (GBCode >= 2433 && GBCode < 2594) return "g";  
    88.             if (GBCode >= 2594 && GBCode < 2787) return "h";  
    89.             if (GBCode >= 2787 && GBCode < 3106) return "j";  
    90.             if (GBCode >= 3106 && GBCode < 3212) return "k";  
    91.             if (GBCode >= 3212 && GBCode < 3472) return "l";  
    92.             if (GBCode >= 3472 && GBCode < 3635) return "m";  
    93.             if (GBCode >= 3635 && GBCode < 3722) return "n";  
    94.             if (GBCode >= 3722 && GBCode < 3730) return "o";  
    95.             if (GBCode >= 3730 && GBCode < 3858) return "p";  
    96.             if (GBCode >= 3858 && GBCode < 4027) return "q";  
    97.             if (GBCode >= 4027 && GBCode < 4086) return "r";  
    98.             if (GBCode >= 4086 && GBCode < 4390) return "s";  
    99.             if (GBCode >= 4390 && GBCode < 4558) return "t";  
    100.             if (GBCode >= 4558 && GBCode < 4684) return "w";  
    101.             if (GBCode >= 4684 && GBCode < 4925) return "x";  
    102.             if (GBCode >= 4925 && GBCode < 5249) return "y";  
    103.             if (GBCode >= 5249 && GBCode <= 5589) return "z";  
    104.             if (GBCode >= 5601 && GBCode <= 8794)  
    105.             {  
    106.                 String CodeData = "cjwgnspgcenegypbtwxzdxykygtpjnmjqmbsgzscyjsyyfpggbzgydywjkgaljswkbjqhyjwpdzlsgmr"  
    107.                 + "ybywwccgznkydgttngjeyekzydcjnmcylqlypyqbqrpzslwbdgkjfyxjwcltbncxjjjjcxdtqsqzycdxxhgckbphffss"  
    108.                 + "pybgmxjbbyglbhlssmzmpjhsojnghdzcdklgjhsgqzhxqgkezzwymcscjnyetxadzpmdssmzjjqjyzcjjfwqjbdzbjgd"  
    109.                 + "nzcbwhgxhqkmwfbpbqdtjjzkqhylcgxfptyjyyzpsjlfchmqshgmmxsxjpkdcmbbqbefsjwhwwgckpylqbgldlcctnma"  
    110.                 + "eddksjngkcsgxlhzaybdbtsdkdylhgymylcxpycjndqjwxqxfyyfjlejbzrwccqhqcsbzkymgplbmcrqcflnymyqmsqt"  
    111.                 + "rbcjthztqfrxchxmcjcjlxqgjmshzkbswxemdlckfsydsglycjjssjnqbjctyhbftdcyjdgwyghqfrxwckqkxebpdjpx"  
    112.                 + "jqsrmebwgjlbjslyysmdxlclqkxlhtjrjjmbjhxhwywcbhtrxxglhjhfbmgykldyxzpplggpmtcbbajjzyljtyanjgbj"  
    113.                 + "flqgdzyqcaxbkclecjsznslyzhlxlzcghbxzhznytdsbcjkdlzayffydlabbgqszkggldndnyskjshdlxxbcghxyggdj"  
    114.                 + "mmzngmmccgwzszxsjbznmlzdthcqydbdllscddnlkjyhjsycjlkohqasdhnhcsgaehdaashtcplcpqybsdmpjlpcjaql"  
    115.                 + "cdhjjasprchngjnlhlyyqyhwzpnccgwwmzffjqqqqxxaclbhkdjxdgmmydjxzllsygxgkjrywzwyclzmcsjzldbndcfc"  
    116.                 + "xyhlschycjqppqagmnyxpfrkssbjlyxyjjglnscmhcwwmnzjjlhmhchsyppttxrycsxbyhcsmxjsxnbwgpxxtaybgajc"  
    117.                 + "xlypdccwqocwkccsbnhcpdyznbcyytyckskybsqkkytqqxfcwchcwkelcqbsqyjqcclmthsywhmktlkjlychwheqjhtj"  
    118.                 + "hppqpqscfymmcmgbmhglgsllysdllljpchmjhwljcyhzjxhdxjlhxrswlwzjcbxmhzqxsdzpmgfcsglsdymjshxpjxom"  
    119.                 + "yqknmyblrthbcftpmgyxlchlhlzylxgsssscclsldclepbhshxyyfhbmgdfycnjqwlqhjjcywjztejjdhfblqxtqkwhd"  
    120.                 + "chqxagtlxljxmsljhdzkzjecxjcjnmbbjcsfywkbjzghysdcpqyrsljpclpwxsdwejbjcbcnaytmgmbapclyqbclzxcb"  
    121.                 + "nmsggfnzjjbzsfqyndxhpcqkzczwalsbccjxpozgwkybsgxfcfcdkhjbstlqfsgdslqwzkxtmhsbgzhjcrglyjbpmljs"  
    122.                 + "xlcjqqhzmjczydjwbmjklddpmjegxyhylxhlqyqhkycwcjmyhxnatjhyccxzpcqlbzwwwtwbqcmlbmynjcccxbbsnzzl"  
    123.                 + "jpljxyztzlgcldcklyrzzgqtgjhhgjljaxfgfjzslcfdqzlclgjdjcsnclljpjqdcclcjxmyzftsxgcgsbrzxjqqcczh"  
    124.                 + "gyjdjqqlzxjyldlbcyamcstylbdjbyregklzdzhldszchznwczcllwjqjjjkdgjcolbbzppglghtgzcygezmycnqcycy"  
    125.                 + "hbhgxkamtxyxnbskyzzgjzlqjdfcjxdygjqjjpmgwgjjjpkjsbgbmmcjssclpqpdxcdyykypcjddyygywchjrtgcnyql"  
    126.                 + "dkljczzgzccjgdyksgpzmdlcphnjafyzdjcnmwescsglbtzcgmsdllyxqsxsbljsbbsgghfjlwpmzjnlyywdqshzxtyy"  
    127.                 + "whmcyhywdbxbtlmswyyfsbjcbdxxlhjhfpsxzqhfzmqcztqcxzxrdkdjhnnyzqqfnqdmmgnydxmjgdhcdycbffallztd"  
    128.                 + "ltfkmxqzdngeqdbdczjdxbzgsqqddjcmbkxffxmkdmcsychzcmljdjynhprsjmkmpcklgdbqtfzswtfgglyplljzhgjj"  
    129.                 + "gypzltcsmcnbtjbhfkdhbyzgkpbbymtdlsxsbnpdkleycjnycdykzddhqgsdzsctarlltkzlgecllkjljjaqnbdggghf"  
    130.                 + "jtzqjsecshalqfmmgjnlyjbbtmlycxdcjpldlpcqdhsycbzsckbzmsljflhrbjsnbrgjhxpdgdjybzgdlgcsezgxlblg"  
    131.                 + "yxtwmabchecmwyjyzlljjshlgndjlslygkdzpzxjyyzlpcxszfgwyydlyhcljscmbjhblyjlycblydpdqysxktbytdkd"  
    132.                 + "xjypcnrjmfdjgklccjbctbjddbblblcdqrppxjcglzcshltoljnmdddlngkaqakgjgyhheznmshrphqqjchgmfprxcjg"  
    133.                 + "dychghlyrzqlcngjnzsqdkqjymszswlcfqjqxgbggxmdjwlmcrnfkkfsyyljbmqammmycctbshcptxxzzsmphfshmclm"  
    134.                 + "ldjfyqxsdyjdjjzzhqpdszglssjbckbxyqzjsgpsxjzqznqtbdkwxjkhhgflbcsmdldgdzdblzkycqnncsybzbfglzzx"  
    135.                 + "swmsccmqnjqsbdqsjtxxmbldxcclzshzcxrqjgjylxzfjphymzqqydfqjjlcznzjcdgzygcdxmzysctlkphtxhtlbjxj"  
    136.                 + "lxscdqccbbqjfqzfsltjbtkqbsxjjljchczdbzjdczjccprnlqcgpfczlclcxzdmxmphgsgzgszzqjxlwtjpfsyaslcj"  
    137.                 + "btckwcwmytcsjjljcqlwzmalbxyfbpnlschtgjwejjxxglljstgshjqlzfkcgnndszfdeqfhbsaqdgylbxmmygszldyd"  
    138.                 + "jmjjrgbjgkgdhgkblgkbdmbylxwcxyttybkmrjjzxqjbhlmhmjjzmqasldcyxyqdlqcafywyxqhz";  
    139.   
    140.                 String _gbcode = GBCode.ToString();  
    141.                 int pos = (Convert.ToInt16(_gbcode.Substring(0, 2)) - 56) * 94 + Convert.ToInt16(_gbcode.Substring(_gbcode.Length - 2, 2));  
    142.                 return CodeData.Substring(pos - 1, 1);  
    143.             }  
    144.             return " ";  
    145.         }  
    146.         //获取汉字的首字母和全拼  
    147.   
    148.         /// <summary>  
    149.         /// 获取汉字的全拼音  
    150.         /// </summary>  
    151.         /// <param name="x">传汉字的字符串</param>  
    152.         /// <returns>汉字的字符串的拼音</returns>  
    153.         public string GetSpell(string x)  
    154.         {  
    155.             int[] iA = new int[]  
    156.               {  
    157.                    -20319 ,-20317 ,-20304 ,-20295 ,-20292 ,-20283 ,-20265 ,-20257 ,-20242 ,-20230  
    158.                    ,-20051 ,-20036 ,-20032 ,-20026 ,-20002 ,-19990 ,-19986 ,-19982 ,-19976 ,-19805  
    159.                    ,-19784 ,-19775 ,-19774 ,-19763 ,-19756 ,-19751 ,-19746 ,-19741 ,-19739 ,-19728  
    160.                    ,-19725 ,-19715 ,-19540 ,-19531 ,-19525 ,-19515 ,-19500 ,-19484 ,-19479 ,-19467  
    161.                    ,-19289 ,-19288 ,-19281 ,-19275 ,-19270 ,-19263 ,-19261 ,-19249 ,-19243 ,-19242  
    162.                    ,-19238 ,-19235 ,-19227 ,-19224 ,-19218 ,-19212 ,-19038 ,-19023 ,-19018 ,-19006  
    163.                    ,-19003 ,-18996 ,-18977 ,-18961 ,-18952 ,-18783 ,-18774 ,-18773 ,-18763 ,-18756  
    164.                    ,-18741 ,-18735 ,-18731 ,-18722 ,-18710 ,-18697 ,-18696 ,-18526 ,-18518 ,-18501  
    165.                    ,-18490 ,-18478 ,-18463 ,-18448 ,-18447 ,-18446 ,-18239 ,-18237 ,-18231 ,-18220  
    166.                    ,-18211 ,-18201 ,-18184 ,-18183 ,-18181 ,-18012 ,-17997 ,-17988 ,-17970 ,-17964  
    167.                    ,-17961 ,-17950 ,-17947 ,-17931 ,-17928 ,-17922 ,-17759 ,-17752 ,-17733 ,-17730  
    168.                    ,-17721 ,-17703 ,-17701 ,-17697 ,-17692 ,-17683 ,-17676 ,-17496 ,-17487 ,-17482  
    169.                    ,-17468 ,-17454 ,-17433 ,-17427 ,-17417 ,-17202 ,-17185 ,-16983 ,-16970 ,-16942  
    170.                    ,-16915 ,-16733 ,-16708 ,-16706 ,-16689 ,-16664 ,-16657 ,-16647 ,-16474 ,-16470  
    171.                    ,-16465 ,-16459 ,-16452 ,-16448 ,-16433 ,-16429 ,-16427 ,-16423 ,-16419 ,-16412  
    172.                    ,-16407 ,-16403 ,-16401 ,-16393 ,-16220 ,-16216 ,-16212 ,-16205 ,-16202 ,-16187  
    173.                    ,-16180 ,-16171 ,-16169 ,-16158 ,-16155 ,-15959 ,-15958 ,-15944 ,-15933 ,-15920  
    174.                    ,-15915 ,-15903 ,-15889 ,-15878 ,-15707 ,-15701 ,-15681 ,-15667 ,-15661 ,-15659  
    175.                    ,-15652 ,-15640 ,-15631 ,-15625 ,-15454 ,-15448 ,-15436 ,-15435 ,-15419 ,-15416  
    176.                    ,-15408 ,-15394 ,-15385 ,-15377 ,-15375 ,-15369 ,-15363 ,-15362 ,-15183 ,-15180  
    177.                    ,-15165 ,-15158 ,-15153 ,-15150 ,-15149 ,-15144 ,-15143 ,-15141 ,-15140 ,-15139  
    178.                    ,-15128 ,-15121 ,-15119 ,-15117 ,-15110 ,-15109 ,-14941 ,-14937 ,-14933 ,-14930  
    179.                    ,-14929 ,-14928 ,-14926 ,-14922 ,-14921 ,-14914 ,-14908 ,-14902 ,-14894 ,-14889  
    180.                    ,-14882 ,-14873 ,-14871 ,-14857 ,-14678 ,-14674 ,-14670 ,-14668 ,-14663 ,-14654  
    181.                    ,-14645 ,-14630 ,-14594 ,-14429 ,-14407 ,-14399 ,-14384 ,-14379 ,-14368 ,-14355  
    182.                    ,-14353 ,-14345 ,-14170 ,-14159 ,-14151 ,-14149 ,-14145 ,-14140 ,-14137 ,-14135  
    183.                    ,-14125 ,-14123 ,-14122 ,-14112 ,-14109 ,-14099 ,-14097 ,-14094 ,-14092 ,-14090  
    184.                    ,-14087 ,-14083 ,-13917 ,-13914 ,-13910 ,-13907 ,-13906 ,-13905 ,-13896 ,-13894  
    185.                    ,-13878 ,-13870 ,-13859 ,-13847 ,-13831 ,-13658 ,-13611 ,-13601 ,-13406 ,-13404  
    186.                    ,-13400 ,-13398 ,-13395 ,-13391 ,-13387 ,-13383 ,-13367 ,-13359 ,-13356 ,-13343  
    187.                    ,-13340 ,-13329 ,-13326 ,-13318 ,-13147 ,-13138 ,-13120 ,-13107 ,-13096 ,-13095  
    188.                    ,-13091 ,-13076 ,-13068 ,-13063 ,-13060 ,-12888 ,-12875 ,-12871 ,-12860 ,-12858  
    189.                    ,-12852 ,-12849 ,-12838 ,-12831 ,-12829 ,-12812 ,-12802 ,-12607 ,-12597 ,-12594  
    190.                    ,-12585 ,-12556 ,-12359 ,-12346 ,-12320 ,-12300 ,-12120 ,-12099 ,-12089 ,-12074  
    191.                    ,-12067 ,-12058 ,-12039 ,-11867 ,-11861 ,-11847 ,-11831 ,-11798 ,-11781 ,-11604  
    192.                    ,-11589 ,-11536 ,-11358 ,-11340 ,-11339 ,-11324 ,-11303 ,-11097 ,-11077 ,-11067  
    193.                    ,-11055 ,-11052 ,-11045 ,-11041 ,-11038 ,-11024 ,-11020 ,-11019 ,-11018 ,-11014  
    194.                    ,-10838 ,-10832 ,-10815 ,-10800 ,-10790 ,-10780 ,-10764 ,-10587 ,-10544 ,-10533  
    195.                    ,-10519 ,-10331 ,-10329 ,-10328 ,-10322 ,-10315 ,-10309 ,-10307 ,-10296 ,-10281  
    196.                    ,-10274 ,-10270 ,-10262 ,-10260 ,-10256 ,-10254  
    197.                   };  
    198.             string[] sA = new string[]  
    199.           {  
    200.            "a","ai","an","ang","ao"  
    201.   
    202.            ,"ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao","bie","bin"  
    203.            ,"bing","bo","bu"  
    204.   
    205.            ,"ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che"  
    206.            ,"chen","cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun"  
    207.            ,"chuo","ci","cong","cou","cu","cuan","cui","cun","cuo"  
    208.   
    209.            ,"da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu"  
    210.            ,"dong","dou","du","duan","dui","dun","duo"  
    211.   
    212.            ,"e","en","er"  
    213.   
    214.            ,"fa","fan","fang","fei","fen","feng","fo","fou","fu"  
    215.   
    216.            ,"ga","gai","gan","gang","gao","ge","gei","gen","geng","gong","gou","gu","gua","guai"  
    217.            ,"guan","guang","gui","gun","guo"  
    218.   
    219.            ,"ha","hai","han","hang","hao","he","hei","hen","heng","hong","hou","hu","hua","huai"  
    220.            ,"huan","huang","hui","hun","huo"  
    221.   
    222.            ,"ji","jia","jian","jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue"  
    223.            ,"jun"  
    224.   
    225.            ,"ka","kai","kan","kang","kao","ke","ken","keng","kong","kou","ku","kua","kuai","kuan"  
    226.            ,"kuang","kui","kun","kuo"  
    227.   
    228.            ,"la","lai","lan","lang","lao","le","lei","leng","li","lia","lian","liang","liao","lie"  
    229.            ,"lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo"  
    230.   
    231.            ,"ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min"  
    232.            ,"ming","miu","mo","mou","mu"  
    233.   
    234.            ,"na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie"  
    235.            ,"nin","ning","niu","nong","nu","nv","nuan","nue","nuo"  
    236.   
    237.            ,"o","ou"  
    238.   
    239.            ,"pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie","pin","ping"  
    240.            ,"po","pu"  
    241.   
    242.            ,"qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que"  
    243.            ,"qun"  
    244.   
    245.            ,"ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo"  
    246.   
    247.            ,"sa","sai","san","sang","sao","se","sen","seng","sha","shai","shan","shang","shao","she"  
    248.            ,"shen","sheng","shi","shou","shu","shua","shuai","shuan","shuang","shui","shun","shuo","si"  
    249.            ,"song","sou","su","suan","sui","sun","suo"  
    250.   
    251.            ,"ta","tai","tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu"  
    252.            ,"tuan","tui","tun","tuo"  
    253.   
    254.            ,"wa","wai","wan","wang","wei","wen","weng","wo","wu"  
    255.   
    256.            ,"xi","xia","xian","xiang","xiao","xie","xin","xing","xiong","xiu","xu","xuan","xue","xun"  
    257.   
    258.            ,"ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you","yu","yuan","yue","yun"  
    259.   
    260.            ,"za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang","zhao"  
    261.            ,"zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui"  
    262.            ,"zhun","zhuo","zi","zong","zou","zu","zuan","zui","zun","zuo"  
    263.           };  
    264.             byte[] B = new byte[2];  
    265.             string s = "";  
    266.             char[] c = x.ToCharArray();  
    267.             for (int j = 0; j < c.Length; j++)  
    268.             {  
    269.                 B = System.Text.Encoding.Default.GetBytes(c[j].ToString());  
    270.                 if ((int)(B[0]) <= 160 && (int)(B[0]) >= 0)  
    271.                 {  
    272.                     s += c[j];  
    273.                 }  
    274.                 else  
    275.                 {  
    276.                     for (int i = (iA.Length - 1); i >= 0; i--)  
    277.                     {  
    278.                         if (iA[i] <= (int)(B[0]) * 256 + (int)(B[1]) - 65536)  
    279.                         {  
    280.                             s += sA[i];  
    281.                             break;  
    282.                         }  
    283.                     }  
    284.                 }  
    285.             }  
    286.             return s;  
    287.         }  
    288.     }  
  • 相关阅读:
    linux部署tomcat服务器
    如何设计功能测试
    sql语句字符串型日期转化为数字类型
    关于软件测试的基础知识
    关于数据库的一些基本知识
    py,先导,--L
    selenium,常用网站
    maven,使用
    移动自动化,appium,java--L
    接口,自动化,java--L
  • 原文地址:https://www.cnblogs.com/weikai/p/3529568.html
Copyright © 2020-2023  润新知