C#中使用微软语言包来获取中文汉字的拼音、拼音首字母、中文繁体
2021/4/30 12:28:19
本文主要是介绍C#中使用微软语言包来获取中文汉字的拼音、拼音首字母、中文繁体,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
首先需要获取ChnCharInfo.dll (汉子转拼音就可以了)和ChineseConverter.dll (简繁转化吧)
ChnCharInfo.dll官方下载操作如下:
先下载微软Microsoft Visual Studio International Pack 1.0 SR1语言包:
Microsoft Visual Studio International Pack 1.0 SR1
Microsoft Visual Studio International Feature Pack 2.0
下载后分别是“vsintlpack1.zip”、“Vsintlpack2.msi”、双击“Vsintlpack2.msi”安装完成之后解压“vsintlpack1.zip”,获得七个语言包。双击其中的“CHSPinYinConv.msi”完成安装后就可以在Visual Studio中引用了。
默认的语言包类库文件引用地址:“C:\Program Files (x86)\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library\ChnCharInfo.dll”
(把这两个dll引入到项目里面.可以写一个YinpinHelper.cs以供调用)
示例:
using引用命名空间:
using Microsoft.International.Converters.PinYinConverter;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.International.Converters.PinYinConverter; using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("汉字转全拼结果:" + GetPinyin("测试")); Console.WriteLine("汉字转全拼首字母结果:" + GetFirstPinyin("测试")); Console.WriteLine("简体转繁体结果:" + GetTraditionalChinese("测试")); Console.WriteLine("繁体转简体结果:" + GetSimplifiedChinese("測試")); Console.ReadLine(); } /// <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[0].ToString(); r += t.Substring(0, t.Length - 1); } 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[0].ToString(); r += t.Substring(0, 1); } catch { r += obj.ToString(); } } return r; } /// <summary> /// 简体中文转化为繁体中文 /// </summary> /// <param name="str">汉字</param> /// <returns>繁体中文</returns> public static string GetTraditionalChinese(string str) { if (string.IsNullOrWhiteSpace(str.Replace(" ", ""))) { return ""; } string result = null; char[] CharSimples = str.ToCharArray(); for (int i = 0; i < CharSimples.Length; i++) { if (ChineseChar.IsValidChar(CharSimples[i])) { result += ChineseConverter.Convert(CharSimples[i].ToString(), ChineseConversionDirection.SimplifiedToTraditional); } else { result += CharSimples[i].ToString(); } } return result; } /// <summary> /// 繁体中文转化为简体中文 /// </summary> /// <param name="str">汉字</param> /// <returns>简体中文</returns> public static string GetSimplifiedChinese(string str) { if (string.IsNullOrWhiteSpace(str.Replace(" ", ""))) { return ""; } string result = null; char[] CharSimples = str.ToCharArray(); for (int i = 0; i < CharSimples.Length; i++) { if (ChineseChar.IsValidChar(CharSimples[i])) { result += ChineseConverter.Convert(CharSimples[i].ToString(), ChineseConversionDirection.TraditionalToSimplified); } else { result += CharSimples[i].ToString(); } } return result; } } }
转: https://blog.csdn.net/qq395537505/article/details/51848681
http://t.zoukankan.com/gguozhenqian-p-4086466.html
这篇关于C#中使用微软语言包来获取中文汉字的拼音、拼音首字母、中文繁体的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#