csharp:汉字转带拼音声调

/// <summary>
         /// �� 塘㙍镇
        ///涂聚文 20130623
        ///http://zh.wiktionary.org/wiki/Wiktionary:%E9%83%A8%E9%A6%96%E7%B4%A2%E5%BC%95
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void button2_Click(object sender, EventArgs e)
         {
             //必須用此設置,否則這樣的字“㙍,��”等輸入進去成亂碼
             this.ImeMode = System.Windows.Forms.ImeMode.On;
 
             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";
             string[] temp = pydic.Split(new char[] { '|' });
 
             foreach (string var in temp)
             {
                 string key = var.Substring(0, 1);
                 string value = var.Substring(1,var.Length-1);
                 hashtable[key] = value;
             }
 
             this.textBox3.Text = ToPinYin(this.textBox2.Text.Trim());
 
             //string[] results = pydic.Split(new[] { '|' }, 2, StringSplitOptions.None);
             //for (int i = 0; i < results.Length; i++)
             //{
             //    Console.WriteLine("\tElement {0}: \"{1}\"", i, results[i]);
             //}
         }
 
         /// <summary>
         ///
         /// </summary>
         /// <param name="hanzi"></param>
         /// <returns></returns>
         public static string ToPinYin(string hanzi)
         {
             StringBuilder sb = new StringBuilder();
 
             foreach (char var in hanzi)
             {
                 string s = hashtable[var.ToString()] as string;
 
                 if (s != null)
                 {
                     sb.Append(s);
                 }
                 else
                 {
                    // MessageBox.Show(s);
                 }
             }
 
             return sb.ToString();
         }

你可能感兴趣的:(csharp:汉字转带拼音声调)