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”



示例:

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();
        }

        ///  
        /// 汉字转化为拼音
        ///  
        /// 汉字 
        /// 全拼 
        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;
        }

        ///  
        /// 汉字转化为拼音首字母
        ///  
        /// 汉字 
        /// 首字母 
        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;
        }

        ///  
        /// 简体中文转化为繁体中文
        ///  
        /// 汉字 
        /// 繁体中文 
        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;
        }

        ///  
        /// 繁体中文转化为简体中文
        ///  
        /// 汉字 
        /// 简体中文 
        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;
        }

    }
}

结果:
C#使用微软语言包来获取中文汉字的拼音、拼音首字母、中文繁体_第1张图片




你可能感兴趣的:(C#)