zoukankan      html  css  js  c++  java
  • .net core 使用PinYinConverterCore 汉字转拼音

    1.全拼音

      public static string ToPinyin(this string msg, bool islower=true)
            {
                string result = string.Empty;
                msg= Regex.Replace(msg, @"[^u4e00-u9fa5]", string.Empty).Trim();
    
                foreach (char item in msg)
                {
                    var cc = new ChineseChar(item);
                    if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
                    {
                        string temp = cc.Pinyins[0].ToString();
                        if (islower)
                        {
                            temp = temp.ToLower();
                        }
                        result += temp.Substring(0, temp.Length - 1);
                    }
                }
                return result;
            }

    2.首字母拼音

       public static string ToFirstPinyin(this string msg, bool islower = true)
            {
                string result = string.Empty;
                msg = Regex.Replace(msg, @"[^u4e00-u9fa5]", string.Empty).Trim();
                foreach (var item in msg)
                {
                    var cc = new ChineseChar(item);
                    if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
                    {
                        string temp = cc.Pinyins[0].ToString();
                        if (islower)
                        {
                            temp = temp.ToLower();
                        }
                        result += temp.Substring(0, 1);
                    }
                }
                return result;
            }
    作者: jamesbing
    提示: 欢迎转载,但是必须保留本文的署名 jamesbing (包含链接)
  • 相关阅读:
    字典常用操作复习
    列表常用方法复习
    爬虫流程复习
    协程解决素数
    yield 复习
    多线程复习2
    多线程复习1
    异常 巩固3
    logging日志基础示例
    2019最新百度网盘不限速下载教程
  • 原文地址:https://www.cnblogs.com/gaobing/p/14485368.html
Copyright © 2011-2022 走看看