C#拼音帮助类
如果使用此帮助类需要引用
using Microsoft.International.Converters.PinYinConverter;
using NPinyin;
可以在NuGet里面下载
1.编码格式为GB2312
- /// <summary>
- /// lou 2019年5月27日10:17:48 Encoding编码
- /// </summary>
- private static readonly Encoding Gb2312 = Encoding.GetEncoding("GB2312");
2.汉字转全拼
- /// <summary>
- ///lou 2019年5月27日10:25:00 汉字转全拼
- /// </summary>
- /// <param name="strChinese">汉字</param>
- /// <returns></returns>
- public static string ConvertToAllSpell(string strChinese, IDictionary<char, string> pinyinDic = null)
- {
- try
- {
- if (strChinese.Length != )
- {
- StringBuilder fullSpell = new StringBuilder();
- for (int i = ; i < strChinese.Length; i++)
- {
- var chr = strChinese[i];
- string pinyin = string.Empty;
- if (pinyin.Length == )
- {
- pinyin = GetSpell(chr);
- }
- fullSpell.Append(pinyin);
- }
- return fullSpell.ToString().ToLower();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("全拼转化出错!" + e.Message);
- }
- return string.Empty;
- }
3.汉字转首字母
- /// <summary>
- /// lou 2019年5月27日10:19:57 汉字转首字母
- /// </summary>
- /// <param name="strChinese">汉字</param>
- /// <returns></returns>
- public static string GetFirstSpell(string strChinese)
- {
- try
- {
- if (strChinese.Length != )
- {
- StringBuilder fullSpell = new StringBuilder();
- for (int i = ; i < strChinese.Length; i++)
- {
- var chr = strChinese[i];
- fullSpell.Append(GetSpell(chr)[]);
- }
- return fullSpell.ToString().ToUpper();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("首字母转化出错!" + e.Message);
- }
- return string.Empty;
- }
4.获取字符拼音
- /// <summary>
- /// lou 2019年5月27日10:20:22 获取字符拼音
- /// </summary>
- /// <param name="chr">字符</param>
- /// <returns></returns>
- private static string GetSpell(char chr)
- {
- var coverchr = Pinyin.GetPinyin(chr);
- bool isChineses = ChineseChar.IsValidChar(coverchr[]);
- if (isChineses)
- {
- ChineseChar chineseChar = new ChineseChar(coverchr[]);
- foreach (string value in chineseChar.Pinyins)
- {
- if (!string.IsNullOrEmpty(value))
- {
- return value.Remove(value.Length - , );
- }
- }
- }
- return coverchr;
- }
C#拼音帮助类的更多相关文章
- 拼音操作工具类 - PinyinUtil.java
拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...
- java PinYinUtils 拼音工具类
package com.sicdt.library.core.utils; import java.util.HashSet; import java.util.Set; import net.sou ...
- C#汉字转拼音帮助类
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...
- 汉字转拼音工具类java
package com.baihui.core.utils; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge. ...
- PHP的UTF-8中文转拼音处理类(性能已优化至极致)
<?php /** * PHP 汉字转拼音 * @author Jerryli(hzjerry@gmail.com) * @version V0.20140715 * @package SPFW ...
- C#工具:汉字转拼音帮助类
using System.Text; namespace Core.Common { /// <summary> /// 取汉字拼音的首字母 /// </summary> pu ...
- PHP的UTF-8中文转拼音处理类
<?php /** * PHP 汉字转拼音 * @author Jerryli(hzjerry@gmail.com) * @version V0.20140715 * @package SPFW ...
- C#中汉字轻松得到拼音全文类
public class chs2py { ,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,- ...
- [Android Pro] 常用的android工具类和库
reference to : http://blog.csdn.net/lovexieyuan520/article/details/50614086 这篇博客主要记录我认为比较有用的Android ...
随机推荐
- JavaScript/JQuery自执行函数
JavaScript中任何库与框架设计的第一个要点就是解决命名空间与变量污染的问题.jQuery就是利用了JavaScript函数作用域的特性,采用自执行函数包裹了自身的方法来解决这个问题.从jQue ...
- oracle 导入导出表
imp username/pwd@orcl file=c:\temp\exp.dmp tables=(table1, table2)#imp username/pwd@ip:1521/orcl ful ...
- Java多线程编程核心技术-第4章-Lock的使用-读书笔记
第 4 章 Lock 的使用 本章主要内容 ReentrantLocal 类的使用. ReentrantReadWriteLock 类的使用. 4.1 使用 ReentrantLock 类 在 Jav ...
- tf–idf算法解释及其python代码
tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...
- IComparable<T>.CompareTo(T) 方法
IComparable<T>.CompareTo(T) 方法 定义 命名空间: System 程序集: System.Runtime.dll, mscorlib.dll, netstand ...
- [codevs2460]树的统计
题目描述 Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHAN ...
- Pandas | 12 选项和自定义
Pandas提供API来自定义其行为的某些方面,大多使用来显示. API由五个相关函数组成.它们分别是: get_option() set_option() reset_option() descri ...
- STL——list用法总结
头文件 #include<list> 声明一个int型的list:list<int> a: 1.list的构造函数 list<int>a{1,2,3} list&l ...
- HttpClient代理IP及设置连接读取超时
1.不废话,上代码: public static void main(String[] args) throws Exception { CloseableHttpClient httpClient ...
- Apache Beam实战指南 | 大数据管道(pipeline)设计及实践
Apache Beam实战指南 | 大数据管道(pipeline)设计及实践 mp.weixin.qq.com 策划 & 审校 | Natalie作者 | 张海涛编辑 | LindaAI 前 ...