C#中文转简拼

nuget引用

 

public static string ChineseCharToPingyin(string str)
        {
            if (null == str || str.Length <= 0)
            {
                return "";
            }
            StringBuilder sb = new StringBuilder("");
            ChineseChar chineseChar = null;
            for (int i = 0; i < str.Length; i++)
            {
                try
                {
                    chineseChar = new ChineseChar(str[i]);
                    if (null != chineseChar.Pinyins[0] && chineseChar.Pinyins[0].Length > 0)
                        sb.Append(chineseChar.Pinyins[0][0]);
                }
                catch
                {
                    sb.Append(str[i]);
                }

            }
            return sb.ToString();
        }

 

posted @ 2022-03-18 09:32  不骄不傲  阅读(110)  评论(0编辑  收藏  举报