此代码属改造而来没有太深的技术难度仅仅是排除了原来代码的错误增加了一些常规需求目前此类可以实现的功能如下
对中文字符串进行拼音转换的基本功能
支持并可自定义拼音单词间的分隔符
支持并可自定义是否将拼音单词首字母大写
支持并可自定义多音词预定音
支持并可自定义转换过程中要排除解析的字符例如中文标点
输出示例
引发 SystemWebUIControlInit 事件以对页进行初始化
YinFa SystemWebUIControlInit ShiJianYiDuiYeJinXingChuShiHua
Hello!重庆和深圳的特色各是什么呢?
Hello!Chong Qing He Shen Zhen De Te Se Ge Shi ShenMe Ne ?
代码如下
/**//// <summary>
/// 汉字转拼音工具
/// </summary>
public sealed class CHSPinYin
{
/**//// <summary>
/// 包含字符 ASC 码的整形数组
/// </summary>
private static int[] pv = new int[] { };
/**//// <summary>
/// 包含汉字拼音的字符串数组
/// </summary>
private static string[] ps = new string[] { a ai an ang ao ba bai ban bang bao bei ben beng bi bian biao bie bin bing bo bu ca cai can cang cao ce ceng cha chai chan chang chao che chen cheng chi chong chou chu chuai chuan chuang chui chun chuo ci cong cou cu cuan cui cun cuo da dai dan dang dao de deng di dian diao die ding diu dong dou du duan dui dun duo e en er fa fan fang fei fen feng fo fou fu ga gai gan gang gao ge gei gen geng gong gou gu gua guai guan guang gui gun guo ha hai han hang hao he hei hen heng hong hou hu hua huai huan huang hui hun huo ji jia jian jiang jiao jie jin jing jiong jiu ju juan jue jun ka kai kan kang kao ke ken keng kong kou ku kua kuai kuan kuang kui kun kuo la lai lan lang lao le lei leng li lia lian liang liao lie lin ling liu long lou lu lv luan lue lun luo ma mai man mang mao me mei men meng mi mian miao mie min ming miu mo mou mu na nai nan nang nao ne nei nen neng ni nian niang niao nie nin ning niu nong nu nv nuan nue nuo o ou pa pai pan pang pao pei pen peng pi pian piao pie pin ping po pu qi qia qian qiang qiao qie qin qing qiong qiu qu quan que qun ran rang rao re ren reng ri rong rou ru ruan rui run ruo sa sai san sang sao se sen seng sha shai shan shang shao she shen sheng shi shou shu shua shuai shuan shuang shui shun shuo si song sou su suan sui sun suo ta tai tan tang tao te teng ti tian tiao tie ting tong tou tu tuan tui tun tuo wa wai wan wang wei wen weng wo wu xi xia xian xiang xiao xie xin xing xiong xiu xu xuan xue xun ya yan yang yao ye yi yin ying yo yong you yu yuan yue yun za zai zan zang zao ze zei zen zeng zha zhai zhan zhang zhao zhe zhen zheng zhi zhong zhou zhu zhua zhuai zhuan zhuang zhui zhun zhuo zi zong zou zu zuan zui zun zuo };
/**//// <summary>
/// 包含要排除处理的字符的字符串数组
/// </summary>
private static string[] bd = new string[] { ¥ $ ( 「 『 ) 」 』 [ 〖 【 ] 〗 】 — … 《 < 》 > };
private static Hashtable _Phrase;
/**//// <summary>
/// 设置或获取包含列外词组读音的键/值对的组合
/// </summary>
public static Hashtable Phrase
{
get
{
if (_Phrase == null)
{
_Phrase = new Hashtable();
_PhraseAdd(重庆 Chong Qing);
_PhraseAdd(深圳 Shen Zhen);
_PhraseAdd(什么 Shen Me);
}
return _Phrase;
}
set { _Phrase = value; }
}
/**//// <summary>
/// 将指定中文字符串转换为拼音形式
/// </summary>
/// <param name=chs>要转换的中文字符串</param>
/// <param name=separator>连接拼音之间的分隔符</param>
/// <param name=initialCap>指定是否将首字母大写</param>
/// <returns>包含中文字符串的拼音的字符串</returns>
public static string Convert(string chs string separator bool initialCap)
{
if (chs == null || chsLength == ) return ;
if (separator == null || separatorLength == ) separator = ;
// 例外词组
foreach (DictionaryEntry de in CHSPinYinPhrase)
{
chs = chsReplace(deKeyToString() StringFormat( {} deValueToString()Replace( separator)));
}
byte[] array = new byte[];
string returnstr = ;
int chrasc = ;
int i = ;
int i = ;
bool b = false;
char[] nowchar = chsToCharArray();
CultureInfo ci = ThreadCurrentThreadCurrentCulture;
TextInfo ti = ciTextInfo;
for (int j = ; j < nowcharLength; j++)
{
array = EncodingDefaultGetBytes(nowchar[j]ToString());
string s = nowchar[j]ToString(); ;
if (arrayLength == )
{
b = true;
returnstr += s;
}
else
{
if (s == ?)
{
if (returnstr == || b == true) returnstr += s;
else returnstr += separator + s;
continue;
}
i = (short)(array[]);
i = (short)(array[]);
chrasc = i * + i ;
for (int i = (pvLength ); i >= ; i)
{
if (pv <= chrasc)
{
s = ps;
if (initialCap == true) s = tiToTitleCase(s);
if (returnstr == || b == true) returnstr += s;
else returnstr += separator + s;
break;
}
}
b = false;
}
}
returnstr = returnstrReplace( separator);
return returnstr;
}
/**//// <summary>
/// 将指定中文字符串转换为拼音形式
/// </summary>
/// <param name=chs>要转换的中文字符串</param>
/// <param name=separator>连接拼音之间的分隔符</param>
/// <returns>包含中文字符串的拼音的字符串</returns>
public static string Convert(string chs string separator)
{
return CHSPinYinConvert(chs separator false);
}
/**//// <summary>
/// 将指定中文字符串转换为拼音形式
/// </summary>
/// <param name=chs>要转换的中文字符串</param>
/// <param name=initialCap>指定是否将首字母大写</param>
/// <returns>包含中文字符串的拼音的字符串</returns>
public static string Convert(string chs bool initialCap)
{
return CHSPinYinConvert(chs initialCap);
}
/**//// <summary>
/// 将指定中文字符串转换为拼音形式
/// </summary>
/// <param name=chs>要转换的中文字符串</param>
/// <returns>包含中文字符串的拼音的字符串</returns>
public static string Convert(string chs)
{
return CHSPinYinConvert(chs );
}
}