.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 ...
随机推荐
- 【一天一道LeetCode】#47. Permutations II
一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...
- [转]高级SQL注入:混淆和绕过
############# [0×00] – 简介[0×01] – 过滤规避(Mysql)[0x01a] – 绕过函数和关键词的过滤[0x01b] – 绕过正则表达式过滤[0×02] – 常见绕过技术 ...
- Linux用户配置文件(第二版)
/etc/passwd文件剖析 文件格式: root:x:0:0:root:/root:/bin/bash 用户名:密码位:UID:GID[缺省组ID]:注释性的描述信息:宿主目录:shell[7部分 ...
- ServletRequest
/** * Defines an object to provide client request information to a servlet. The * servlet container ...
- ReentrantReadWriteLock读写锁的使用1
本文可作为传智播客<张孝祥-Java多线程与并发库高级应用>的学习笔记. 一个简单的例子 两个线程,一个不断打印a,一个不断打印b public class LockTest { publ ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
- Linux - 用make进行工程编译
首先建立好自己的工作目录 然后创建主函数main.cpp 接着写sinValue.h和cosValue.h函数文件 先按照传统方式进行编译运行 然后用make,先写makefile文件 将原来生成的文 ...
- how tomcat works 总结 二
第五章 servlet容器 第 5 章讨论 container 模块.container 指的是 org.apache.catalina.Container 接口,有4 种类型的 container: ...
- 面试之路(27)-链表中倒数第K个结点
代码的鲁棒性: 所谓的鲁棒性是指能够判断输入是否合乎规范,能对不和规范的程序进行处理. 容错性是鲁棒性的一个重要体现. 防御性编程有助于提高鲁棒性. 切入正题,我可不是标题党: 链表倒数第k个节点 列 ...
- rubygem若干常用选项参数
可以用gem help commands看所有支持的参数,这个比gem -h显示的全: wisy@wisy-ThinkPad-X61:~/src/ruby_src$ gem help commands ...