首页 > 技术文章 > 01ChineseChar类的用法---------Microsoft.International.Converters.PinYinConverter 命名空间包含了拼音和笔画转换工具的类。

clcloveHuahua 2016-01-22 16:20 原文

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             while (true)
 6             {
 7                 Console.WriteLine("请输入你要转的汉字");
 8                 string str = Console.ReadLine();
 9                 string py = GetPinYin(str);
10                 Console.WriteLine(py);
11             }
12 
13             //ChineseChar类的其他用法:
14             //1、静态方法:
15             //    ->GetCharCount(Inter16)        检索具有指定笔画数的字符个数。
16             //    ->GetChars(string)                返回具有相同的指定拼音的字符串列表
17             //    ->GetChars(Inter16)            返回具有指定笔画的所有汉字。           
18             //    ->GetHomophoneCount(string)    检索具有指定拼音的字符数。 
19             //    ->GetStrokeNumber(Char)        检索指定字符的笔画数。 
20             //    ->IsHomophone(Char, Char)        识别给出的两个字符是否是同音字。
21             //    ->IsValidChar(Char)            识别给出的字符串是否是一个有效的汉字字符。  
22             //    ->IsValidPinyin(String)        识别给出的拼音是否是一个有效的拼音字符串。 
23             //    ->IsValidStrokeNumber(Int16)    识别给出的笔画数是否是一个有效的笔画数。
24             //2、实例方法:
25             //    ->ChineseCharNew(Char)            ChineseChar类的构造函数。 
26             //    ->CompareStrokeNumber(Char)    将给出的字符和实例字符的笔画数进行比较。  
27             //    ->HasSound(String)                识别字符是否有指定的读音。 
28             //    -> IsHomophone(Char)            识别给出的字符是否是实例字符的同音字。
29             //3、实例属性:
30             //    ->ChineseCharacter                获取这个汉字字符。  
31             //    ->IsPolyphone                    获取这个字符是否是多音字。  
32             //    ->PinyinCount                    获取这个字符的拼音个数。 
33             //    ->StrokeNumber                    获取这个字符的笔画数
34         }
35         /// <summary>
36         /// 获得中文字符串的拼音
37         /// </summary>
38         /// <param name="str"></param>
39         /// <returns></returns>
40         private static string GetPinYin(string str)
41         {
42             string py = "";
43 
44             for (int i = 0; i < str.Length; i++)
45             {
46                 if (ChineseChar.IsValidChar(str[i]))//判断当前字符是不是汉字
47                 {
48                     ChineseChar cc = new ChineseChar(str[i]);//构造方法             
49                     py += cc.Pinyins[0].TrimEnd('1', '2', '3', '4', '5').ToLower();
50                 }
51                 else//不是汉字的话 加本身
52                 {
53                     py += str[i];
54                 }
55 
56             }
57 
58             return py;
59         }
60     }

 

推荐阅读