csharp:汉字转带拼音声调

来源:互联网 发布:mac os x 版本 编辑:程序博客网 时间:2024/03/29 03:50
 1          ///   <summary>
 2           ///  �� 塘㙍镇
 3          /// 涂聚文 20130623
 4          /// http://zh.wiktionary.org/wiki/Wiktionary :%E9%83%A8%E9%A6%96%E7%B4%A2%E5%BC%95
 5           ///   </summary>
 6           ///   <param name="sender"></param>
 7           ///   <param name="e"></param>
 8           private  void button2_Click( object sender, EventArgs e)
 9         {
10              // 必須用此設置,否則這樣的字“㙍,��”等輸入進去成亂碼
11               this.ImeMode = System.Windows.Forms.ImeMode.On;
12 
13              string pydic= " 吖ā|阿ā|啊ā|锕ā|錒ā|嗄á|厑ae|哎āi|哀āi|樉shuǎng|谁shuí|鹴shuāng|爽shuǎng|鏯shuǎng|孀shuāng|孇shuāng|騻shuāng|焋zhuàng|幢zhuàng|撞zhuàng|隹zhuī|傱shuǎn|㙍duō|kuǎi|zhǒu ";
14              string[] temp = pydic.Split( new  char[] {  ' | ' });
15 
16              foreach ( string  var  in temp)
17             {
18                  string key =  var.Substring( 01);
19                  string value =  var.Substring( 1, var.Length- 1);
20                 hashtable[key] = value;
21             }
22 
23              this.textBox3.Text = ToPinYin( this.textBox2.Text.Trim());
24 
25              // string[] results = pydic.Split(new[] { '|' }, 2, StringSplitOptions.None);
26               // for (int i = 0; i < results.Length; i++)
27               // {
28               //     Console.WriteLine("\tElement {0}: \"{1}\"", i, results[i]);
29               // }
30          }
31 
32          ///   <summary>
33           ///
34           ///   </summary>
35           ///   <param name="hanzi"></param>
36           ///   <returns></returns>
37           public  static  string ToPinYin( string hanzi)
38         {
39             StringBuilder sb =  new StringBuilder();
40 
41              foreach ( char  var  in hanzi)
42             {
43                  string s = hashtable[ var.ToString()]  as  string;
44 
45                  if (s !=  null)
46                 {
47                     sb.Append(s);
48                 }
49                  else
50                 {
51                     //  MessageBox.Show(s);
52                  }
53             }
54 
55              return sb.ToString();
56         }

<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>