教你如何将中文转换成全拼

在转换之前,我们先要判断输入的是否为中文,中文字符处于4e00与9fff之间,了解了这个知识之后,我们开始操作

     /// <summary>
        /// 转化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int code = 0;
            int chfrom = Convert.ToInt32("4e00",16);
            int chend = Convert.ToInt32("9fff",16);

            //全拼功能
            //this.textBox2.Text = Pinyin.GetPinyin(this.textBox1.Text.ToString());
            //this.textBox2.Text = this.textBox2.Text.Replace(" ", "");
            for (int i = 0; i < this.textBox1.Text.Length; i++)
            {
                this.textBox2.Text += this.textBox1.Text[i].ToString() + ": " + Pinyin.GetPinyin(this.textBox1.Text[i].ToString());
            }
            Encoding gb2312 = Encoding.GetEncoding("GB2312");
            for (int i = 0; i < this.textBox1.Text.Length; i++)
            {
                string s = Pinyin.ConvertEncoding(this.textBox1.Text[i].ToString(), Encoding.UTF8, gb2312);
                this.textBox2.Text += this.textBox1.Text[i].ToString() + Pinyin.GetInitials(s, gb2312);
            }
            for (int i = 0; i < this.textBox1.Text.Length; i++)
            {
                code = Char.ConvertToUtf32(this.textBox1.Text[i].ToString(), 0);
                if (code >= chfrom && code <= chend)
                    this.textBox2.Text += this.textBox1.Text[i].ToString() + " / 中文";
            }
            

        }

ok,这里的转换已分为多种,难度可以说没有,微软已经大大降低了开发人员的开发难度,有利于开发人员将精力投放于业务逻辑。

posted on 2013-03-05 11:12  源坊  阅读(713)  评论(0编辑  收藏  举报

导航