中文转拼音全拼和首字母

需要引用ms的ChnCharInfo.dll文件

中文转拼音全拼:characters为多个中文字的字符串

private static string GetPinYing(string characters)
        {

            if(characters.Length != 0)
            {

                StringBuilder fullSpellBuild = new StringBuilder();

                for(int i = 0; i < characters.Length; i++)
                {
                    //判断是否是中文
                    bool itemFlag = ChineseChar.IsValidChar(characters[i]);

                    if(itemFlag)
                    {
                        ChineseChar chineseChar = new ChineseChar(characters[i]);
                        foreach(string value in chineseChar.Pinyins)
                        {
                            if(!string.IsNullOrEmpty(value))
                            {

                                fullSpellBuild.Append(value.Remove(value.Length - 1, 1));
                                break;
                            }
                        }
                    }
                    else
                    {

                        fullSpellBuild.Append(characters[i]);
                    }
                }


                return fullSpellBuild.ToString();
            }
            return "";
        }


中文转拼音首字母:chineseString为多个中文字的字符串

#region 中文转拼音首字母
        private static readonly string[] RegionChar = new string[32]
            {
                "CJWGNSPGCGNESYPBTYYZDXYKYGTDJNNJQMBSGZSCYJSYYQPGKBZGYCYWJKGKLJSWKPJQHYTWDDZLSGMRYPYWWCCKZNKYDG",
                "TTNGJEYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYXJWZLTBNCXJJJJZXDTTSQZYCDXXHGCKBPHFFSSWYBGMXLPBYLLLHLX",
                "SPZMYJHSOJNGHDZQYKLGJHSGQZHXQGKEZZWYSCSCJXYEYXADZPMDSSMZJZQJYZCDJZWQJBDZBXGZNZCPWHKXHQKMWFBPBY",
                "DTJZZKQHYLYGXFPTYJYYZPSZLFCHMQSHGMXXSXJJSDCSBBQBEFSJYHWWGZKPYLQBGLDLCCTNMAYDDKSSNGYCSGXLYZAYBN",
                "PTSDKDYLHGYMYLCXPYCJNDQJWXQXFYYFJLEJBZRXCCQWQQSBNKYMGPLBMJRQCFLNYMYQMSQTRBCJTHZTQFRXQHXMJJCJLX",
                "QGJMSHZKBSWYEMYLTXFSYDSGLYCJQXSJNQBSCTYHBFTDCYZDJWYGHQFRXWCKQKXEBPTLPXJZSRMEBWHJLBJSLYYSMDXLCL",
                "QKXLHXJRZJMFQHXHWYWSBHTRXXGLHQHFNMNYKLDYXZPWLGGTMTCFPAJJZYLJTYANJGBJPLQGDZYQYAXBKYSECJSZNSLYZH",
                "ZXLZCGHPXZHZNYTDSBCJKDLZAYFMYDLEBBGQYZKXGLDNDNYSKJSHDLYXBCGHXYPKDJMMZNGMMCLGWZSZXZJFZNMLZZTHCS",
                "YDBDLLSCDDNLKJYKJSYCJLKOHQASDKNHCSGANHDAASHTCPLCPQYBSDMPJLPCJOQLCDHJJYSPRCHNWJNLHLYYQYYWZPTCZG",
                "WWMZFFJQQQQYXACLBHKDJXDGMMYDJXZLLSYGXGKJRYWZWYCLZMSSJZLDBYDCFCXYHLXCHYZJQSFQAGMNYXPFRKSSBJLYXY",
                "SYGLNSCMHCWWMNZJJLXXHCHSYDSTTXRYCYXBYHCSMXJSZNPWGPXXTAYBGAJCXLYSDCCWZOCWKCCSBNHCPDYZNFCYYTYCKX",
                "KYBSQKKYTQQXFCWCHCYKELZQBSQYJQCCLMTHSYWHMKTLKJLYCXWHEQQHTQHZPQSQSCFYMMDMGBWHWLGSSLYSDLMLXPTHMJ",
                "HWLJZYHZJXHTXJLHXRSWLWZJCBXMHZQXSDZPMGFCSGLSXYMJSHXPJXWMYQKSMYPLRTHBXFTPMHYXLCHLHLZYLXGSSSSTCL",
                "SLDCLRPBHZHXYYFHBBGDMYCNQQWLQHJJZYWJZYEJJDHPBLQXTQKWHLCHQXAGTLXLJXMSLXHTZKZJECXJCJNMFBYCSFYWYB",
                "JZGNYSDZSQYRSLJPCLPWXSDWEJBJCBCNAYTWGMPAPCLYQPCLZXSBNMSGGFNZJJBZSFZYNDXHPLQKZCZWALSBCCJXJYZGWK",
                "YPSGXFZFCDKHJGXDLQFSGDSLQWZKXTMHSBGZMJZRGLYJBPMLMSXLZJQQHZYJCZYDJWBMJKLDDPMJEGXYHYLXHLQYQHKYCW",
                "CJMYYXNATJHYCCXZPCQLBZWWYTWBQCMLPMYRJCCCXFPZNZZLJPLXXYZTZLGDLDCKLYRZZGQTGJHHHJLJAXFGFJZSLCFDQZ",
                "LCLGJDJCSNCLLJPJQDCCLCJXMYZFTSXGCGSBRZXJQQCTZHGYQTJQQLZXJYLYLBCYAMCSTYLPDJBYREGKLZYZHLYSZQLZNW",
                "CZCLLWJQJJJKDGJZOLBBZPPGLGHTGZXYGHZMYCNQSYCYHBHGXKAMTXYXNBSKYZZGJZLQJDFCJXDYGJQJJPMGWGJJJPKQSB",
                "GBMMCJSSCLPQPDXCDYYKYFCJDDYYGYWRHJRTGZNYQLDKLJSZZGZQZJGDYKSHPZMTLCPWNJAFYZDJCNMWESCYGLBTZCGMSS",
                "LLYXQSXSBSJSBBSGGHFJLWPMZJNLYYWDQSHZXTYYWHMCYHYWDBXBTLMSYYYFSXJCSDXXLHJHFSSXZQHFZMZCZTQCXZXRTT",
                "DJHNNYZQQMNQDMMGYYDXMJGDHCDYZBFFALLZTDLTFXMXQZDNGWQDBDCZJDXBZGSQQDDJCMBKZFFXMKDMDSYYSZCMLJDSYN",
                "SPRSKMKMPCKLGDBQTFZSWTFGGLYPLLJZHGJJGYPZLTCSMCNBTJBQFKTHBYZGKPBBYMTTSSXTBNPDKLEYCJNYCDYKZDDHQH",
                "SDZSCTARLLTKZLGECLLKJLQJAQNBDKKGHPJTZQKSECSHALQFMMGJNLYJBBTMLYZXDCJPLDLPCQDHZYCBZSCZBZMSLJFLKR",
                "ZJSNFRGJHXPDHYJYBZGDLQCSEZGXLBLGYXTWMABCHECMWYJYZLLJJYHLGBDJLSLYGKDZPZXJYYZLWCXSZFGWYYDLYHCLJS",
                "CMBJHBLYZLYCBLYDPDQYSXQZBYTDKYXJYYCNRJMPDJGKLCLJBCTBJDDBBLBLCZQRPPXJCGLZCSHLTOLJNMDDDLNGKAQHQH",
                "JGYKHEZNMSHRPHQQJCHGMFPRXHJGDYCHGHLYRZQLCYQJNZSQTKQJYMSZSWLCFQQQXYFGGYPTQWLMCRNFKKFSYYLQBMQAMM",
                "MYXCTPSHCPTXXZZSMPHPSHMCLMLDQFYQXSZYJDJJZZHQPDSZGLSTJBCKBXYQZJSGPSXQZQZRQTBDKYXZKHHGFLBCSMDLDG",
                "DZDBLZYYCXNNCSYBZBFGLZZXSWMSCCMQNJQSBDQSJTXXMBLTXZCLZSHZCXRQJGJYLXZFJPHYMZQQYDFQJJLZZNZJCDGZYG",
                "CTXMZYSCTLKPHTXHTLBJXJLXSCDQXCBBTJFQZFSLTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZ",
                "GSZZQJYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNDSZFDEQFHBS",
                "AQTGLLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ"
            };
        private  static readonly Encoding Encoding = Encoding.GetEncoding("GB2312");
        private  static readonly List<string> StrList = new List<string>();

        public static string GetFirstPinYin(string chineseString)
        {
            if (!string.IsNullOrEmpty(chineseString))
            {
                TextElementEnumerator stringList = StringInfo.GetTextElementEnumerator(chineseString);           
                var firstPinYin = new StringBuilder();
                while (stringList.MoveNext())
                {
                    StrList.Add(stringList.GetTextElement());
                }
                foreach (var stringChar in StrList)
                {
                    firstPinYin.Append(GetFirstChar(stringChar));
                }
                StrList.Clear();
                return firstPinYin.ToString();
            }
            return null;
        }

        private static char GetFirstChar(string chineseChar)
        {
            var bytes = Encoding.GetBytes(chineseChar);
            if (bytes.Length != 2)
                return chineseChar[0];
            return GetChar(bytes[0], bytes[1], chineseChar);
        }

        private static char GetChar(byte c1, byte c2, string originChar)
        {
            var Hi = c1 << 8;
            var Lo = c2;
            int n = Hi + Lo;
            if (n <= 0xD7F9)
            {
                if (In(0xB0A1, 0xB0C4, n)) return 'A';
                if (In(0XB0C5, 0XB2C0, n)) return 'B';
                if (In(0xB2C1, 0xB4ED, n)) return 'C';
                if (In(0xB4EE, 0xB6E9, n)) return 'D';
                if (In(0xB6EA, 0xB7A1, n)) return 'E';
                if (In(0xB7A2, 0xB8C0, n)) return 'F';
                if (In(0xB8C1, 0xB9FD, n)) return 'G';
                if (In(0xB9FE, 0xBBF6, n)) return 'H';
                if (In(0xBBF7, 0xBFA5, n)) return 'J';
                if (In(0xBFA6, 0xC0AB, n)) return 'K';
                if (In(0xC0AC, 0xC2E7, n)) return 'L';
                if (In(0xC2E8, 0xC4C2, n)) return 'M';
                if (In(0xC4C3, 0xC5B5, n)) return 'N';
                if (In(0xC5B6, 0xC5BD, n)) return 'O';
                if (In(0xC5BE, 0xC6D9, n)) return 'P';
                if (In(0xC6D1, 0xC8BA, n)) return 'Q';
                if (In(0xC8BB, 0xC8F5, n)) return 'R';
                if (In(0xC8F6, 0xCBF9, n)) return 'S';
                if (In(0xCBFA, 0xCDD9, n)) return 'T';
                if (In(0xCDDA, 0xCEF3, n)) return 'W';
                if (In(0xCEF4, 0xD1B8, n)) return 'X';
                if (In(0xD1B9, 0xD4D0, n)) return 'Y';
                if (In(0xD4D1, 0xD7F9, n)) return 'Z';
                return originChar[0];
            }

            var b1 = (c1 & 0x7F) - 0x20 - 56;
            var b2 = (c2 & 0x7F) - 0x20 - 1;
            if (b1 >= 0 && b1 <= 31 && b2 >= 0 && b2 <= 93)
            {
                return RegionChar[b1][b2];
            }
            return originChar[0];
        }

        private static bool In(int lp, int hp, int value)
        {
            return ((value <= hp) && (value >= lp));
        }
        #endregion




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值