using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Text;
namespace COMCMS.Common
{
public class PinYinHelper
{
///
/// 默认编码
///
//private static Encoding gb2312 = Encoding.GetEncoding("GB2312");
#region 汉字转化为拼音
///
/// 汉字转化为拼音
///
/// 汉字
/// 全拼
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;
}
#endregion
#region 汉字转化为拼音首字母
///
/// 汉字转化为拼音首字母
///
/// 汉字
/// 首字母
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;
}
#endregion
}
}