通过类库ChineseChar实现将汉字转化为拼音
//封装dll
using Microsoft.International.Converters.PinYinConverter;
using System.Text;
namespace Utils
{
public static class ChnCharInfo {
//原始
public static string ChinaCharInfoConsonant(string ToString)
{
StringBuilder SB = new StringBuilder();
foreach (char item in ToString.ToCharArray())
{
//有效
if (ChineseChar.IsValidChar(item))
{
ChineseChar China = new ChineseChar(item);
SB.Append(China.Pinyins[0]);
}
}
return SB.ToString();
}
//字母转大写去掉声调数字
public static string ChinaCharInfoUpper(string ToString)
{
StringBuilder SB = new StringBuilder();
foreach (char item in ToString.ToCharArray())
{
if (ChineseChar.IsValidChar(item))
{
ChineseChar China = new ChineseChar(item);
SB.Append(China.Pinyins[0].Substring(0,China.Pinyins[0].Length-1));
}
}
return SB.ToString();
}
//转小写
public static string ChinaCharInfoLower(string ToString)
{
StringBuilder SB = new StringBuilder();
foreach (char item in ToString.ToCharArray())
{
if (ChineseChar.IsValidChar(item))
{
ChineseChar China = new ChineseChar(item);
SB.Append(China.Pinyins[0].Substring(0, China.Pinyins[0].Length - 1).ToLower());
}
}
return SB.ToString();
}
//首字母大写
public static string ChinaCharInfoFirst(string ToString)
{
StringBuilder SB = new StringBuilder();
foreach (char item in ToString.ToCharArray())
{
if (ChineseChar.IsValidChar(item))
{
ChineseChar China = new ChineseChar(item);
string Temp = China.Pinyins[0].Substring(0, China.Pinyins[0].Length - 1);
SB.Append(Temp.Substring(0,1).ToUpper()+ (China.Pinyins[0].Substring(1, China.Pinyins[0].Length - 2)).ToLower());
}
}
return SB.ToString();
}
}
}
posted on 2017-03-18 19:04 Ran[Crazy2016] 阅读(946) 评论(0) 编辑 收藏 举报