首先引入ChnCharInfo.dll 第3方的一个库

代码:

btn_chinChar_Click事件:

        private void btn_chinChar_Click(object sender, EventArgs e)
        {
            ChineseChar cr =null;
            string str = "", txtString = txt_string.Text.Trim();
            if (!string.IsNullOrEmpty(txtString))
            {
                foreach (var item in cr.GetChineseSpellings(txtString))
                {
                    str += item + "-";
                }
            }
            MessageBox.Show(str);
        }

扩展方法:

 public static class ChineseCharacters
    {
        public static ICollection<string> GetChineseSpellings(this ChineseChar chinChar ,string value)
        {
            List<string> list;
            int i,start;
            char c;

            if (string.IsNullOrEmpty(value))return null;

            start = 0;
            list = new List<string>();

            for (i = 0; i < value.Length; ++i)
            {
                c = value[i];
                if (ChineseChar.IsValidChar(c))
                {
                    if (i > start)
                    {
                        list.Add(value.Substring(start, i - start));
                    }
                    chinChar = new ChineseChar(c);
                    list.Add(chinChar.Pinyins.First().Substring(0, chinChar.Pinyins.First().Length - 1).ToLower());
                    start = i + 1;
                }
            }

            if (i > start)
            {
                list.Add(value.Substring(start, i - start));
            }
            return list;
        }
    }

运用结果:

C# 汉字转化拼音

相关文章:

  • 2022-02-04
  • 2021-07-26
  • 2021-10-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2021-06-23
  • 2021-05-29
  • 2021-07-27
相关资源
相似解决方案