.NET Core使用微软官方类库实现汉字转拼音
一、NuGet包
拼音:Install-Package PinYinConverterCore
简体-繁体互转:Install-Package TraditionalChineseToSimplifiedConverter
二、C#代码
class Program
{ static void Main(string[] args)
{ string Name= "刘大大"; Console.WriteLine($"{Name}\n全拼:{GetPinyin(Name)},\n首拼:{GetFirstPinyin(Name)},\n繁体字:{GetTraditional(Name)},\n简体字:{GetSimplified(GetTraditional(Name))}"); Console.ReadKey();
} /// <summary>
/// 汉字转化为拼音
/// </summary>
/// <param name="str">汉字</param>
/// <returns>全拼</returns>
public static string GetPinyin(string str)
{
string r = string.Empty;
foreach (char obj in str)
{
try
{
ChineseChar chineseChar = new ChineseChar(obj);
string t = chineseChar.Pinyins[].ToString();
r += t.Substring(, t.Length - );
}
catch
{
r += obj.ToString();
}
}
return r;
} /// <summary>
/// 汉字转化为拼音首字母
/// </summary>
/// <param name="str">汉字</param>
/// <returns>首字母</returns>
public static string GetFirstPinyin(string str)
{
string r = string.Empty;
foreach (char obj in str)
{
try
{
ChineseChar chineseChar = new ChineseChar(obj);
string t = chineseChar.Pinyins[].ToString();
r += t.Substring(, );
}
catch
{
r += obj.ToString();
}
}
return r;
} // <summary>
/// 简体转换为繁体
/// </summary>
/// <param name="str">简体字</param>
/// <returns>繁体字</returns>
public static string GetTraditional(string str)
{
string r = string.Empty;
r = ChineseConverter.Convert(str, ChineseConversionDirection.SimplifiedToTraditional);
return r;
}
/// <summary>
/// 繁体转换为简体
/// </summary>
/// <param name="str">繁体字</param>
/// <returns>简体字</returns>
public static string GetSimplified(string str)
{
string r = string.Empty;
r = ChineseConverter.Convert(str, ChineseConversionDirection.TraditionalToSimplified);
return r;
} #endregion }
}
运行结果

.NET Core使用微软官方类库实现汉字转拼音的更多相关文章
- [转]使用微软的官方类库CHSPinYinConv获得汉字拼音
原文链接:http://outofmemory.cn/code-snippet/4392/ms-CHSPinYinConv-convert-hanzi-to-pinyin 微软为中文,日文以及韩文提供 ...
- 微软官方实例 RazorPagesMovie 在 asp.net core 2.1 版本下的实战
微软官方实例 RazorPagesMovie 在 asp.net core 2.1 版本下的实战 友情提示: 操作系统: MacOS 10.13.5 dotnet core: version 2.1. ...
- 微软官方NET Core 2.0
NET Core 2.0 微软官方发布的.NET Core 2.0相关的博客: Announcing .NET Standard 2.0 Announcing .NET Core 2.0 F# and ...
- 一大波开发者福利来了,一份微软官方Github上发布的开源项目清单等你签收
目录 微软Github开源项目入口 微软开源项目受欢迎程度排名 Visual Studio Code TypeScript RxJS .NET Core 基础类库 CNTK Microsoft cal ...
- Win10微软官方最终正式版ISO镜像文件
Win10微软官方最终正式版ISO镜像文件 据说Windows 10是微软发布的最后一个Windows版本,下一代Windows将作为Update形式出现.Windows 10将发布7个发行版本,分别 ...
- 微软官方SqlHelper类 数据库辅助操作类
数据库操作类真的没有必要自己去写,因为成熟的类库真的非常完善了,拿来直接用就好,省时省力. 本文就为大家介绍微软官方的程序PetShop4.0中的SqlHelper类,先来做一下简单的介绍,PetSh ...
- 温故知新,微软官方推荐的Visual Studio源代码管理之Git Ignore清单,开启新项目必备宝书
什么是Git Ignore清单 https://git-scm.com/docs/gitignore 简单来说,在Git进行源代码管理中,我们可以通过建立.gitignore来实现一个忽略的黑名单管理 ...
- Windows7微软官方原版镜像系统文件
Windows7微软官方原版镜像系统 Windows 7 是由微软公司(Microsoft)开发的操作系统,核心版本号为Windows NT 6.1.Windows 7可供家庭及 商业工作环境.笔记本 ...
- Windows7下的免费虚拟机(微软官方虚拟机)
前言: 不是说windows7自带的虚拟机最好用,而是他是正式版的,免费的,只要你是windows7用户,就可以免费使用: 其实我最推荐的还是Vmware: 微软为什么提供免费的虚拟机呢? 因为vis ...
随机推荐
- 让你的动画不再生硬 Android插值器Interpolator使用秘籍
有木有厌烦生硬的动画效果,想不想让你的动画变得圆滑且 欢迎收看本期的走进科学... 停,停,别打了,(.﹏.*) 我错了-- 我们要达到的效果: 先来回顾一下普通动画的用法吧. * 缩放动画 Scal ...
- nginx 编译增加新的模块
原已经安装好的nginx,现在需要添加一个未被编译安装的模块: nginx -V 可以查看原来编译时都带了哪些参数 原来的参数:--prefix=/app/nginx 添加的参数: --with-ht ...
- HDTV(1920x1080)码率和视频质量关系的研究 1 (前期准备)
Hans Hoffmann等人在论文<Studies on the Bit Rate Requirements for a HDTV Format With 1920 x 1080 pixel ...
- Android不同系统版本依然能调用到正确的API方法Demo——Service调用startForeground举例
private static final Class<?>[] mSetForegroundSignature = new Class[] { boolean.class}; privat ...
- vim多行增加缩进
http://blog.163.com/clevertanglei900@126/blog/static/11135225920116891750734/ 在Normal Mode下,命令>&g ...
- PHP 查询脚本
POST查询以表格传参数支持中文,GET不支持. POST查询: <?php $id=$_POST["id"];//id(中括号)为传来的参数,$id为php中的变量 //l ...
- Unity Socket UDP
using System.Collections; using System.Collections.Generic; using System.Net.Sockets; using System.N ...
- objective-c中所谓的僵尸对象
正常情况下向已回收的对象发送消息时灵时不灵,具体要看该对象所占内存有没有被覆写.cocoa提供了僵尸对象(Zombie Object)这个功能,简单的说:启用该调试功能后,运行时会将所有已回收的实例转 ...
- 查询oracle数据库的数据库名、实例名、ORACLE_SID
数据库名.实例名.数据库域名.全局数据库名.服务名 , 这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水.我们现在就来把它们弄个明白. 一.数据库名 什么是数 ...
- How--to-deploy-smart-contracts-on
The following smart contract code is only an example and is NOT to be used in Production systems. pr ...