zoukankan      html  css  js  c++  java
  • .net core汉字转拼音或拼音首字母

    首先在项目中引入nuget包NPinyin.Core,然后新建静态转换类,即可在中畅用。

     1 public static class PinYinCast
     2     {        
     3             private static Encoding gb2312 = Encoding.GetEncoding("GB2312");
     4 
     5             /// <summary>
     6             /// 汉字转大写全拼
     7             /// </summary>
     8             /// <param name="strChinese"></param>
     9             /// <returns></returns>
    10             public static string ConvertToAllSpell(string strChinese)
    11             {
    12                 try
    13                 {
    14                     if (strChinese.Length != 0)
    15                     {
    16                         StringBuilder fullSpell = new StringBuilder();
    17                         for (int i = 0; i < strChinese.Length; i++)
    18                         {
    19                             var chr = strChinese[i];
    20                             fullSpell.Append(GetSpell(chr));
    21                         }
    22                         return fullSpell.ToString().ToUpper();//转小写方法为 .ToLower()
    23                     }
    24                 }
    25                 catch (Exception e)
    26                 {
    27                     Console.WriteLine("出错!" + e.Message);
    28                 }
    29                 return string.Empty;
    30             }
    31 
    32             /// <summary>
    33             /// 汉字转首字母大写
    34             /// </summary>
    35             /// <param name="strChinese"></param>
    36             /// <returns></returns>
    37             public static string GetFirstSpell(string strChinese)
    38             {
    39                 try
    40                 {
    41                     if (strChinese.Length != 0)
    42                     {
    43                         StringBuilder fullSpell = new StringBuilder();
    44                         for (int i = 0; i < strChinese.Length; i++)
    45                         {
    46                             var chr = strChinese[i];
    47                             fullSpell.Append(GetSpell(chr)[0]);
    48                         }
    49                         return fullSpell.ToString().ToUpper();//转小写方法为 .ToLower()
    50                     }
    51                 }
    52                 catch (Exception e)
    53                 {
    54                     Console.WriteLine("出错!" + e.Message);
    55                 }
    56 
    57                 return string.Empty;
    58             }
    59 
    60             private static string GetSpell(char chr)
    61             {
    62                 var coverchr = NPinyin.Pinyin.GetPinyin(chr);
    63                 return coverchr;
    64             }
    65         }
  • 相关阅读:
    spring整合curator实现分布式锁
    curator操作zookeeper
    zk创建集群
    zookeeper下的基本操作
    java语音转文字
    netty的数据通信之心跳检测
    arm B和BL指令浅析
    NAND FLASH驱动程序
    外设位宽为8、16、32时,CPU与外设之间地址线的连接方法
    内存接口原理图笔记
  • 原文地址:https://www.cnblogs.com/destinyyuan/p/12760397.html
Copyright © 2011-2022 走看看