C#拼音帮助类
2024-09-06 09:59:47
如果使用此帮助类需要引用
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;
}
最新文章
- CF2.C
- Apache服务器在80端口配置多域名虚拟主机的方法
- MVC系列2-Model
- 关于处理小数点位数的几个oracle函数
- JavaScript 对象的创建和对6种继承模式的理解和遐想
- PAT 1030. 完美数列(25)
- sublime text2 打开包含中文的文件会自动追加.dump后缀解决办法
- Lattice 的 Framebuffer IP核使用调试笔记之IP核生成与参数设置
- C#:实现托盘
- AutoLayout +Masonary
- MySQL 5.7 深度解析: 临时表空间
- C# 带用户密码访问网络共享
- 【转】关于C execlp函数的理解
- 如何把一个TXT文本文件按行数分割成多个文本文件
- MySQL实例
- Oracle.ManagedDataAccess.dll 连接Oracle数据库不需要安装客户端
- OO(Object Oriented)
- javascript 作用域详解
- js利用sort()方法实现数组排序
- 表情存储异常--mybatis抛出异常(java.sql.SQLException: Incorrect string value: &#39;\xF0\x9F\x92\x94&#39; for column &#39;name&#39; at row 1)
热门文章
- PHP技术栈
- 逆向破解之160个CrackMe —— 028
- Mybatis-plus中的condition条件
- axios get及post方法代码示例&;&;方法封装
- css 布局 一中一右
- this指向问题(改变它的指向)
- ESA2GJK1DH1K升级篇: 远程升级准备工作: 使用TCP客户端连接Web服务器实现http下载数据
- vue工具 - vue/cli@3.xx 安装使用流程
- Tarjan 算法求 LCA / Tarjan 算法求强连通分量
- 图、流程图、ER图怎么画及常见画图工具(流程图文章汇总)