.Net、C# 汉字转拼音,简体繁体转换方法

Visual Studio International Pack 包含一组类库,该类库扩展了.NET Framework对全球化软件开发的支持。使用该类库提供的类,.NET 开发人员可以更方便的创建支持多文化多语言的软件应用
下载地址

(1) (ChnCharInfo.dll)

Simplified Chinese Pin-Yin Conversion Library

  • 支持获取简体中文字符的常用属性比如拼音,多音字,同音字,笔画数。

【例如:】

Microsoft.International.Converters.PinYinConverter.ChineseChar cc=new Microsoft.International.Converters.PinYinConverter.ChineseChar('国');

(2)(ChineseConverter.dll)

Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool

  • 支持简繁体中文之间的转换。该组件还包含一个Visual Studio集成开发环境中的插件(Add-in)支持简繁体中文资源文件之间的转换。

【例如:】

–简体转换为繁体字

string temp_1 = Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter.ChineseConverter.Convert("中华人民共和国", 
Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter.ChineseConversionDirection.SimplifiedToTraditional);

–繁体字转换为简体

string temp_2 = Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter.ChineseConverter.Convert(temp_1, 
Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter.ChineseConversionDirection.TraditionalToSimplified);

(3)(EastAsiaNumericFormatter.dll)

East Asia Numeric Formatting Library - 支持将小写的数字字符串格式化成简体中文,繁体中文,日文和韩文的大写数字字符串。
【例如:】

–将数字转换为大写简体中文(拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾点肆伍)

string temp_4 = string.Format(new Microsoft.International.Formatters.EastAsiaNumericFormatter(), "{0:L}", 1234567890.45); 

–将数字转换为小写(十二亿三千四百五十六万七千八百九十点四五)

string temp_6 = string.Format(new Microsoft.International.Formatters.EastAsiaNumericFormatter(), "{0:Ln}", 1234567890.45);

–将数字转换为货币(拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾点肆伍)

string temp_7 = string.Format(new Microsoft.International.Formatters.EastAsiaNumericFormatter(), "{0:Lc}", 1234567890.45);

实例代码:

//简体/繁体切换
string temp_1 = ChineseConverter.Convert("中国人", ChineseConversionDirection.TraditionalToSimplified);
string temp_2 = ChineseConverter.Convert("中国人", ChineseConversionDirection.SimplifiedToTraditional);
Console.WriteLine("简体转换:"+temp_1+"\n繁体转换:"+temp_2);
//汉字转换拼音
string r = string.Empty;
Console.Write("请输入任意汉字:");
string str = Console.ReadLine();
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();
    }
}
Console.WriteLine(r.ToLower().ToString());

源码:
在这里插入图片描述
结果:
在这里插入图片描述


使用微软语言包实现汉字转拼音

网上有很多汉字转拼音的方案,但并不知道性能和可靠性如何。所以本着选择权威的原则,还是想找一个官方的解决方案。下面实用微软官方的语言包实现汉字转拼音。

下载地址:http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=44cac7f0-633b-477d-aed2-99aee642fc10&DisplayLang=zh-cn

此安装包包含日语、汉语、韩语等多种语言,选择安装CHSPinYinConv.msi。完成之后在项目中引用安装目录中的ChnCharInfo.dll即可。

下面就是实现汉字转拼音的demo

using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Suspects.Dll.Util
{
    
    
    publicclassPinYinUtil
    {
    
    
        //返回字符串的简拼
        publicstaticstring getSimplePinYin(stringinputTxt)
        {
    
    
            string shortR ="";
            foreach (char cin inputTxt.Trim())
            {
    
    
                ChineseChar chineseChar =newChineseChar(c);
                shortR +=chineseChar.Pinyins[0].Substring(0, 1).ToLower();
            }
            return shortR;
        }
 
        //返回字符串全拼
        publicstaticstring getAllPinYin(string inputTxt)
        {
    
    
            string allR ="";
            foreach (char cin inputTxt.Trim())
            {
    
    
                ChineseChar chineseChar =newChineseChar(c);
                allR += chineseChar.Pinyins[0].Substring(0,chineseChar.Pinyins[0].Length - 1).ToLower();
            }
            return allR;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/WuLex/article/details/107514243
今日推荐