C#把汉字转换成拼音

1.下载下面类库并引入里面的三个DLL文件

https://files.cnblogs.com/xdoudou/Microsoft_Visual_Studio_International_Pack_1.0.rar

2.创建一个类Pinyin.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;

using Microsoft.International.Converters.PinYinConverter;
using System.Collections.ObjectModel;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;


/// <summary>
/// 汉字转拼音类
/// </summary>
public class PinYinConver
{
    public PinYinConver()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 把汉字转换成拼音(全拼)
    /// by 
    /// </summary>
    /// <param name="hzString">汉字字符串</param>
    /// <returns>转换后的拼音(全拼)字符串</returns>
    public static string Convert(string hzString)
    {
        if (string.IsNullOrEmpty(hzString))
            return "";

        char[] noWChar = hzString.ToCharArray();
        string txt = "";
        for (int j = 0; j < noWChar.Length; j++)
        {
            if (IsValidChar(noWChar[j]))
            {
                txt += ConvertToFirstPinYin(noWChar[j].ToString());
            }
        }
        return txt;
    }
    public static bool IsValidChar(char ch)
    {
        return ChineseChar.IsValidChar(ch);
    }
    public static string ConvertToFirstPinYin(string chineseStr)
    {
        if (chineseStr == null)
            return "";

        char[] charArray = chineseStr.ToCharArray();
        StringBuilder sb = new StringBuilder();
        foreach (char c in charArray)
        {
            ChineseChar chineseChar = new ChineseChar(c);
            ReadOnlyCollection<string> pyColl = chineseChar.Pinyins;
            foreach (string py in pyColl)
            {
                if (py != null)
                {
                    sb.Append(py.Substring(0, 1));
                    break;
                }
            }
        }
        return sb.ToString();
    }
    public static string ConvertToPinYin(string chineseStr, bool includeTone)
    {
        if (chineseStr == null)
            throw new ArgumentNullException("chineseStr");

        char[] charArray = chineseStr.ToCharArray();
        StringBuilder sb = new StringBuilder();
        foreach (char c in charArray)
        {
            ChineseChar chineseChar = new ChineseChar(c);
            ReadOnlyCollection<string> pyColl = chineseChar.Pinyins;
            foreach (string py in pyColl)
            {
                if (py == null)
                {
                    continue;
                }
                sb.Append(py);
                sb.Append("&");
                
            }
            sb.Remove(sb.Length-1,1);
        }

        if (!includeTone)
        {
            StringBuilder sb2 = new StringBuilder();
            foreach (char c in sb.ToString())
            {
                if (!char.IsNumber(c))
                    sb2.Append(c);
            }
            return sb2.ToString();
        }

        return sb.ToString();
    } 
}

3.调用相应的静态方法进行转换

posted @ 2013-05-04 15:57  Xdoudou  阅读(291)  评论(0编辑  收藏  举报