C# 把汉字转换成拼音(全拼)

来源:互联网 发布:像淘宝一样的购物平台 编辑:程序博客网 时间:2024/04/27 17:42



        public static string ChineseToFullPinYin(string chinese)        {            // 匹配中文字符            Regex regex = new Regex("^[\u4e00-\u9fa5]$");            byte[] array = new byte[2];            string pyString = "";            int chrAsc = 0;            int i1 = 0;            int i2 = 0;            char[] noWChar = chinese.ToCharArray();            for (int j = 0; j < noWChar.Length; j++)            {                // 中文字符                if (regex.IsMatch(noWChar[j].ToString()))                {                    array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());                    i1 = (short)(array[0]);                    i2 = (short)(array[1]);                    chrAsc = i1 * 256 + i2 - 65536;                    if (chrAsc > 0 && chrAsc < 160)                    {                        pyString += noWChar[j];                    }                    else                    {                        // 修正部分文字                        if (chrAsc == -9254) // 修正"圳"字                            pyString += "Zhen";                        else if (chrAsc == -12080)                        {                            pyString += "Hang";                        }                        else                        {                            for (int i = (pyValue.Length - 1); i >= 0; i--)                            {                                if (pyValue[i] <= chrAsc)                                {                                    pyString += pyName[i];                                    break;                                }                            }                        }                    }                }                // 非中文字符                else                {                    pyString += noWChar[j].ToString();                }            }            return pyString;        }


0 0
原创粉丝点击