ASP.NET 实现在线中文繁简转换

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{

    StringBuilder GetJWord = new StringBuilder();
    StringBuilder GetFWord = new StringBuilder();
    /// <summary>
    /// 以上是用StringBuilder来操作的同也可以用数组
    /// 下面是用字典来操作
    /// </summary>
     Dictionary<string, int> dicf = new Dictionary<string,int>();
     Dictionary<string, int> dicj  = new Dictionary<string, int>();
  
    protected void Page_Load(object sender, EventArgs e)
    {
       
           DataTable dt=Add_Data();
           if (dt.Rows.Count == 0)
           {
               Response.Write("存中没有数据");
           }
           else if (dt.Rows.Count==2)
           {
               Response.Write("数据有了可以进行转换了!");
               foreach (DataRow item in dt.Rows)
               {
                       if (item[1].ToString() == "简体字")
                       {
                           for (int i = 0; i < item[2].ToString().Length; i++)
                           {
                               dicj.Add(item[2].ToString().Substring(i, 1), i);
                           }
                          
                          
                          GetJWord.Append(item[2].ToString());
                       }
                       if(item[1].ToString()=="繁体字")
                       {
                           for (int i = 0; i < item[2].ToString().Length; i++)
                           {
                               if (!dicf.ContainsKey(item[2].ToString().Substring(i, 1)))
                               {
                                   dicf.Add(item[2].ToString().Substring(i, 1), i);
                               }
                           }

                           GetFWord.Append(item[2].ToString());
                       }
               }
                
                  
               }
             


           }

 
  
    public DataTable Add_Data()
    {
        string str_con = "server=.\\sqlexpress;initial catalog=FUJIA;integrated security=true";
        using (SqlConnection con = new SqlConnection(str_con))
        {
            using (SqlCommand cmd = new SqlCommand("select * from  JTOF", con))
            {  con.Open();
                using (SqlDataReader myreader = cmd.ExecuteReader())
                {
                 
                    DataTable dt = new DataTable();
                    dt.Load(myreader);
                    return dt;
                }


            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
           // Response.Write();
           FileUpload1.PostedFile.SaveAs(Server.MapPath("txt")+"/123.txt");
            FileStream fs = new FileStream(Server.MapPath("txt") + "/123.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs,System.Text.Encoding.Default);
            string s = "";
            StringBuilder sbjianti = new StringBuilder();
            StringBuilder sbfangti = new StringBuilder();
            string fa = "";
            string ji = "";
            int ss= 0;
            while ((s=sr.ReadLine())!=null)
            {
               // Response.Write(s+"<br/>");
                if ("繁体字:" == s)
                {
                   //Response.Write("OKOKO"+"<br/>");
                    ji = "繁体字";
                    ss = 1;
                    continue;
                }
                else if ("简体字:" == s)
                {
                    fa = "简体字";
                    ss = 0;
                    continue;
                }
                if (ss==1)
                {
                    sbfangti.Append(s);
                }
                else if (ss == 0)
                {
                    sbjianti.Append(s);
               
                }
            }
            sr.Close();
            fs.Close();
          ADD_Word(ji,sbfangti.ToString());
          ADD_Word(fa,sbjianti.ToString());

    }

    private static void ADD_Word(string title,string content)
    {
            string str_con = "server=.\\sqlexpress;initial catalog=FUJIA;integrated security=true";
            using (SqlConnection con=new SqlConnection(str_con))
            {
                using (SqlCommand cmd = new SqlCommand("insert JTOF values(@title,@content)",con))
                {
                    con.Open();
                    cmd.Parameters.Add("@title",title);
                    cmd.Parameters.Add("@content",content);
                    cmd.ExecuteNonQuery();
                   
                }
            }
    }
   protected void Button2_Click(object sender, EventArgs e)
   {
      TextBox2.Text = "";
           for (int i = 0; i < TextBox1.Text.Length; i++)
   {
               string word=TextBox1.Text.Substring(i,1);
      Swithc(word);
   }
      
   
   }

   private void Swithc(string word)
   {
       if (TextBox1.Text != "")
       {
           if (GetJWord.ToString().Contains(word))
           {

               for (int i = 0; i < GetJWord.Length; i++)
               {
                   if (GetJWord[i].ToString() == word)
                   {
                       TextBox2.Text += GetFWord[i].ToString();
                       break;

                   }
               }
           }
           else
           {
               for (int i = 0; i < GetFWord.Length; i++)
               {
                   if (GetFWord[i].ToString() == word)
                   {
                       TextBox2.Text += GetJWord[i].ToString();
                       break;

                   }
               }

           }


       }
       else
       {
           Response.Write("不能为空!");
       }
   }

   protected void Button3_Click(object sender, EventArgs e)
   {
       TextBox2.Text = "";
       for (int i = 0; i < TextBox1.Text.Length; i++)
       {
         Switch_Dic(TextBox1.Text.Substring(i,1));   
       }
    
   
   }

   private void Switch_Dic(string word)
   {
       if (dicj.ContainsKey(word))
       {
           int i = 0;   //这将远远高物前者很多倍
           if (dicj.TryGetValue(word, out i))   //字典实现只JT ————FT从F————J也一样
           {
               TextBox2.Text += GetFWord[i].ToString();
           }
       }
       else
       {
           int i = 0;   //这将远远高物前者很多倍
           if (dicf.TryGetValue(word, out i))   //字典实现只JT ————FT从F————J也一样
           {
               TextBox2.Text += GetJWord[i].ToString();
           }

       }
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值