根據中文名,自動生成首字母的拼音碼或拼音碼(兩種方法)


 #region  根據中文生成首字母(大寫)
        /// <summary>
        /// 根據中文生成首字母(大寫)
        /// </summary>
        /// <param name="strString"></param>
        /// <returns></returns>
        public static string GetPingying(string strString)
        {
            string tempStr = "";
            //去掉字符串中的特殊符號
            var str = Regex.Replace(strString, "[ \\[ \\] \\^ \\-_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "");
            foreach (char c in str)
            {
                if ((int)c >= 33 && (int)c <= 126)
                {//字母轉換為大學   
                    tempStr += c.ToString().ToUpper();
                }
                else
                {//累加拼音聲母   
                    tempStr += GetCharSpellCode(c.ToString());
                }
            }
            return tempStr;
        }

        /// <summary>
        /// 得到一個漢字的拼音第一個字母,如果是一個英文字母則直接返回大寫字母
        /// </summary>
        /// <param name="CnChar">單個漢字</param>
        /// <returns>單個大寫字母</returns>
        private static string GetCharSpellCode(string CnChar)
        {
            long iCnChar;
            byte[] ZW = System.Text.Encoding.Default.GetBytes(CnChar);
            //如果是字母,則直接返回首字母
            if (ZW.Length == 1)
            {
                return CnChar.ToUpper().Substring(0, 1);
            }
            else
            {
                // get the array of byte from the single char
                int i1 = (short)(ZW[0]);
                int i2 = (short)(ZW[1]);
                iCnChar = i1 * 256 + i2;
            }
            // iCnChar match the constant
            if ((iCnChar >= 45217) && (iCnChar <= 45252))
            {
                return "A";
            }
            else if ((iCnChar >= 45253) && (iCnChar <= 45760))
            {
                return "B";
            }
            else if ((iCnChar >= 45761) && (iCnChar <= 46317))
            {
                return "C";
            }
            else if ((iCnChar >= 46318) && (iCnChar <= 46825))
            {
                return "D";
            }
            else if ((iCnChar >= 46826) && (iCnChar <= 47009))
            {
                return "E";
            }
            else if ((iCnChar >= 47010) && (iCnChar <= 47296))
            {
                return "F";
            }
            else if ((iCnChar >= 47297) && (iCnChar <= 47613))
            {
                return "G";
            }
            else if ((iCnChar >= 47614) && (iCnChar <= 48118))
            {
                return "H";
            }
            else if ((iCnChar >= 48119) && (iCnChar <= 49061))
            {
                return "J";
            }
            else if ((iCnChar >= 49062) && (iCnChar <= 49323))
            {
                return "K";
            }
            else if ((iCnChar >= 49324) && (iCnChar <= 49895))
            {
                return "L";
            }
            else if ((iCnChar >= 49896) && (iCnChar <= 50370))
            {
                return "M";
            }
            else if ((iCnChar >= 50371) && (iCnChar <= 50613))
            {
                return "N";
            }
            else if ((iCnChar >= 50614) && (iCnChar <= 50621))
            {
                return "O";
            }
            else if ((iCnChar >= 50622) && (iCnChar <= 50905))
            {
                return "P";
            }
            else if ((iCnChar >= 50906) && (iCnChar <= 51386))
            {
                return "Q";
            }
            else if ((iCnChar >= 51387) && (iCnChar <= 51445))
            {
                return "R";
            }
            else if ((iCnChar >= 51446) && (iCnChar <= 52217))
            {
                return "S";
            }
            else if ((iCnChar >= 52218) && (iCnChar <= 52697))
            {
                return "T";
            }
            else if ((iCnChar >= 52698) && (iCnChar <= 52979))
            {
                return "W";
            }
            else if ((iCnChar >= 52980) && (iCnChar <= 53688))
            {
                return "X";
            }
            else if ((iCnChar >= 53689) && (iCnChar <= 54480))
            {
                return "Y";
            }
            else if ((iCnChar >= 54481) && (iCnChar <= 55289))
            {
                return "Z";
            }
            else
                //return ("?");
                return ("");
        }
        #endregion

注意:可能有一些字識別不了,“呃”就不能識別

 

第二種方法:

根據微軟語言包將漢字轉換為拼音,需要引入微軟的插件:ChnCharInfo.dll

插件下載地址:https://files.cnblogs.com/files/likui-bookHouse/Microsoft.International.Converters.PinYinConverter.1.0.0.rar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.International.Converters.PinYinConverter;

namespace HYS.BT.AppSrv.Common.Util
{
    /// <summary>
    /// 拼音轉換
    /// </summary>
    public static class PinyinConverter
    {
        #region 漢字轉化為拼音
        /// <summary> 
        /// 漢字轉化為拼音
        /// </summary> 
        /// <param name="str">漢字</param> 
        /// <returns>全拼</returns> 
        public static string GetPinyin(string str)
        {
            string r = string.Empty;
            foreach (char obj in str)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(obj);

                    string t = chineseChar.Pinyins[0].ToString();
                    r += t.Substring(0, t.Length - 1);
                }
                catch
                {
                    r += obj.ToString();
                }
            }
            return r;
        }
        #endregion

        #region 漢字轉化為拼音首字母
        /// <summary> 
        /// 漢字轉化為拼音首字母
        /// </summary> 
        /// <param name="str">漢字</param> 
        /// <returns>首字母</returns> 
        public static string GetFirstPinyin(string str)
        {
            string r = string.Empty;
            foreach (char obj in str)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(obj);
                    string t = chineseChar.Pinyins[0].ToString();
                    r += t.Substring(0, 1);
                }
                catch
                {
                    r += obj.ToString();
                }
            }
            return r;
        }
        #endregion
    }
}

 注意:如果是數字,英文就原樣返回,有的漢字獲取的拼音有問題。如“廣”

如圖:"廣"我追蹤后發現被翻譯成了"AN"

果然,中國漢字是世界難題啊!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM