如果使用此帮助类需要引用

      using Microsoft.International.Converters.PinYinConverter;
      using NPinyin;

可以在NuGet里面下载

1.编码格式为GB2312

  1.     /// <summary>
  2. /// lou 2019年5月27日10:17:48 Encoding编码
  3. /// </summary>
  4. private static readonly Encoding Gb2312 = Encoding.GetEncoding("GB2312");

2.汉字转全拼

  1. /// <summary>
  2. ///lou 2019年5月27日10:25:00 汉字转全拼
  3. /// </summary>
  4. /// <param name="strChinese">汉字</param>
  5. /// <returns></returns>
  6. public static string ConvertToAllSpell(string strChinese, IDictionary<char, string> pinyinDic = null)
  7. {
  8. try
  9. {
  10. if (strChinese.Length != )
  11. {
  12. StringBuilder fullSpell = new StringBuilder();
  13. for (int i = ; i < strChinese.Length; i++)
  14. {
  15. var chr = strChinese[i];
  16. string pinyin = string.Empty;
  17. if (pinyin.Length == )
  18. {
  19. pinyin = GetSpell(chr);
  20. }
  21. fullSpell.Append(pinyin);
  22. }
  23.  
  24. return fullSpell.ToString().ToLower();
  25. }
  26. }
  27. catch (Exception e)
  28. {
  29. Console.WriteLine("全拼转化出错!" + e.Message);
  30. }
  31.  
  32. return string.Empty;
  33. }

3.汉字转首字母

  1. /// <summary>
  2. /// lou 2019年5月27日10:19:57 汉字转首字母
  3. /// </summary>
  4. /// <param name="strChinese">汉字</param>
  5. /// <returns></returns>
  6. public static string GetFirstSpell(string strChinese)
  7. {
  8. try
  9. {
  10. if (strChinese.Length != )
  11. {
  12. StringBuilder fullSpell = new StringBuilder();
  13. for (int i = ; i < strChinese.Length; i++)
  14. {
  15. var chr = strChinese[i];
  16. fullSpell.Append(GetSpell(chr)[]);
  17. }
  18.  
  19. return fullSpell.ToString().ToUpper();
  20. }
  21. }
  22. catch (Exception e)
  23. {
  24. Console.WriteLine("首字母转化出错!" + e.Message);
  25. }
  26.  
  27. return string.Empty;
  28. }

4.获取字符拼音

  1. /// <summary>
  2. /// lou 2019年5月27日10:20:22 获取字符拼音
  3. /// </summary>
  4. /// <param name="chr">字符</param>
  5. /// <returns></returns>
  6. private static string GetSpell(char chr)
  7. {
  8. var coverchr = Pinyin.GetPinyin(chr);
  9. bool isChineses = ChineseChar.IsValidChar(coverchr[]);
  10. if (isChineses)
  11. {
  12. ChineseChar chineseChar = new ChineseChar(coverchr[]);
  13. foreach (string value in chineseChar.Pinyins)
  14. {
  15. if (!string.IsNullOrEmpty(value))
  16. {
  17. return value.Remove(value.Length - , );
  18. }
  19. }
  20. }
  21.  
  22. return coverchr;
  23. }

C#拼音帮助类的更多相关文章

  1. 拼音操作工具类 - PinyinUtil.java

    拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...

  2. java PinYinUtils 拼音工具类

    package com.sicdt.library.core.utils; import java.util.HashSet; import java.util.Set; import net.sou ...

  3. C#汉字转拼音帮助类

    using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...

  4. 汉字转拼音工具类java

    package com.baihui.core.utils; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge. ...

  5. PHP的UTF-8中文转拼音处理类(性能已优化至极致)

    <?php /** * PHP 汉字转拼音 * @author Jerryli(hzjerry@gmail.com) * @version V0.20140715 * @package SPFW ...

  6. C#工具:汉字转拼音帮助类

    using System.Text; namespace Core.Common { /// <summary> /// 取汉字拼音的首字母 /// </summary> pu ...

  7. PHP的UTF-8中文转拼音处理类

    <?php /** * PHP 汉字转拼音 * @author Jerryli(hzjerry@gmail.com) * @version V0.20140715 * @package SPFW ...

  8. C#中汉字轻松得到拼音全文类

    public class chs2py { ,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,- ...

  9. [Android Pro] 常用的android工具类和库

    reference to  : http://blog.csdn.net/lovexieyuan520/article/details/50614086 这篇博客主要记录我认为比较有用的Android ...

随机推荐

  1. JavaScript/JQuery自执行函数

    JavaScript中任何库与框架设计的第一个要点就是解决命名空间与变量污染的问题.jQuery就是利用了JavaScript函数作用域的特性,采用自执行函数包裹了自身的方法来解决这个问题.从jQue ...

  2. oracle 导入导出表

    imp username/pwd@orcl file=c:\temp\exp.dmp tables=(table1, table2)#imp username/pwd@ip:1521/orcl ful ...

  3. Java多线程编程核心技术-第4章-Lock的使用-读书笔记

    第 4 章 Lock 的使用 本章主要内容 ReentrantLocal 类的使用. ReentrantReadWriteLock 类的使用. 4.1 使用 ReentrantLock 类 在 Jav ...

  4. tf–idf算法解释及其python代码

    tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...

  5. IComparable<T>.CompareTo(T) 方法

    IComparable<T>.CompareTo(T) 方法 定义 命名空间: System 程序集: System.Runtime.dll, mscorlib.dll, netstand ...

  6. [codevs2460]树的统计

    题目描述 Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I.                    CHAN ...

  7. Pandas | 12 选项和自定义

    Pandas提供API来自定义其行为的某些方面,大多使用来显示. API由五个相关函数组成.它们分别是: get_option() set_option() reset_option() descri ...

  8. STL——list用法总结

    头文件 #include<list> 声明一个int型的list:list<int> a: 1.list的构造函数 list<int>a{1,2,3} list&l ...

  9. HttpClient代理IP及设置连接读取超时

    1.不废话,上代码: public static void main(String[] args) throws Exception { CloseableHttpClient httpClient ...

  10. Apache Beam实战指南 | 大数据管道(pipeline)设计及实践

    Apache Beam实战指南 | 大数据管道(pipeline)设计及实践  mp.weixin.qq.com 策划 & 审校 | Natalie作者 | 张海涛编辑 | LindaAI 前 ...