c# 汉字转拼音
来源:互联网 发布:c语言俄罗斯方块代码 编辑:程序博客网 时间:2024/04/28 14:31
最近开发一个类似于OUTLOOK联系人那样可以根据字母来过滤当前的联系人的模块,使用上了微软不久前提供的一个转换汉字为拼音的开发包,可以通过这里进行下载。使用的方法很简单,不过里面的方法都是对于一个汉字使用而言,而且会包含音调,譬如“张”,转换出来就会是“ZHANG1”,“学”,转换出来就会是“XUE2”,如此类推。因此自己再封装一下达到可以整个姓名转换成拼音,并去掉没用的音调。代码如下:
- public class PinYinConverter
- {
- /// <summary>
- /// 将字符串转换成拼音
- /// </summary>
- /// <param name="chineseStr">姓名字符串</param>
- /// <param name="includeTone">是否包含音调</param>
- /// <returns></returns>
- 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)
- {
- sb.Append(py);
- }
- }
- 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();
- }
- public static string ConvertToPinYin(string chineseStr)
- {
- return ConvertToPinYin(chineseStr, true);
- }
- public static bool IsValidChar(char ch)
- {
- return ChineseChar.IsValidChar(ch);
- }
- /// <summary>
- /// 是否为有效的中文字
- /// </summary>
- /// <param name="chn"></param>
- /// <returns></returns>
- public static bool IsValidChinese(string chn)
- {
- if (chn == null)
- throw new ArgumentNullException("chn");
- foreach (char c in chn)
- {
- if (!IsValidChar(c))
- return false;
- }
- return true;
- }
- }
- c# 汉字转拼音
- c#汉字转拼音
- C# 汉字转拼音
- c#汉字转拼音
- C# 汉字转拼音
- c# 汉字转拼音
- C# 汉字转拼音
- C# 汉字转拼音
- c# 汉字转拼音(完整的拼音)
- C# 汉字转拼音(转)
- c#的汉字转拼音
- c#的汉字转拼音
- C#汉字转拼音类
- 汉字转拼音_gb2312 C#
- C#汉字转拼音缩写
- C#汉字转拼音首字母
- [转]c#拼音汉字转化
- C#汉字转拼音源码
- 《XIM 企业即时通讯软件》
- 硬盘安装ubuntu 8.04 版:在xp系统上的引导安装
- WPF随想--无边框播放器1
- 影响个人职业前途的不良习惯
- ORA-01502: 索引或这类索引的分区处于不可用状态
- c# 汉字转拼音
- 判断插于U盘
- Linux下Telnet服务的安装与配置
- 處理 "df" and "du" 不一致的結果
- vc中注册表的一些简单操作
- 文档占用空间du和df的差别
- setjmp使用例子
- 与Applet第一次亲密接触
- 服务器空间满的处理: