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         }
  • 相关阅读:
    Java实现 计蒜客 拯救行动
    Java实现 计蒜客 拯救行动
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 173 二叉搜索树迭代器
    Java实现 LeetCode 173 二叉搜索树迭代器
    Visual Studio的SDK配置
    怎样使用CMenu类
    mfc menu用法一
  • 原文地址:https://www.cnblogs.com/destinyyuan/p/12760397.html
Copyright © 2011-2022 走看看