通过类库ChineseChar实现将汉字转化为拼音
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过类库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();
}
}
}
以上是关于通过类库ChineseChar实现将汉字转化为拼音的主要内容,如果未能解决你的问题,请参考以下文章