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 ...
随机推荐
- python处理excel函数xlrd、xlwt
https://www.jianshu.com/p/f2c9dff344c6 https://www.cnblogs.com/ilovepython/p/11068841.html 行列操作:http ...
- WebVTT字幕格式
[时间:2019-05] [状态:Open] [关键词:字幕,vtt,webvtt, 文件格式,cue,css] 0 引言 WebVTT(Web Video Text Tracks),通过HTML5中 ...
- Python 和 R 中的一数多图
R # 一数多图 x <- 2:6 y <- 7:3 y1 <- y +2 opar <- par(no.readonly = TRUE) par(mfrow=c(2, 3)) ...
- NLP学习(5)----attention/ self-attention/ seq2seq/ transformer
目录: 1. 前提 2. attention (1)为什么使用attention (2)attention的定义以及四种相似度计算方式 (3)attention类型(scaled dot-produc ...
- MySol序
1.mysql是基于C/S端的服务器软件 mysql服务端: server端开启,对文件的增删改查 , musql客户端::连接S端, 发送指令 isnert into t1 values(1); 2 ...
- nginx 缓存服务
1.nginx 缓存 upstream imooc { server 116.62.103.228:8001; server 116.62.103.228:8002; server 116.62.10 ...
- Django-Model操作数据库
查询 models.UserInfo.objects.all() models.UserInfo.objects.all().values('user') #只取user列 models.UserIn ...
- Python并发编程之进程通信
''' 进程间的通信 ''' """ multiprocessing模块支持进程间通信的两种主要形式:管道和队列 都是基于消息传递实现的, ""&qu ...
- 访问阿里云 vue项目的 127.0.0.1:+ip
vue 127.0.0.1 将 localhost 改为 0.0.0.0 原理看下面链接 参考: https://www.cnblogs.com/sddai/p/9281189.html
- TensorFlow Lite for Android示例
一.TensorFlow Lite TensorFlow Lite 是用于移动设备和嵌入式设备的轻量级解决方案.TensorFlow Lite 支持 Android.iOS 甚至树莓派等多种平台. ...