加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#控件textBox行中数据去重复方法

(2012-03-25 21:49:00)
标签:

杂谈



        private void button1_Click(object sender, EventArgs e)

        {

            quchongfu(textBox1);

        }

        //去除重复项

        int chongfu;

        private void quchongfu(TextBox tb)//textBox控件名称

        {

            chongfu = 0;

            for (int i = 0; i < tb.Lines.Length; i++)

            {

                for (int j = i + 1; j < tb.Lines.Length; j++)

                {

                    Delay(100);//延时执行

                    if (tb.Lines[i].Equals(tb.Lines[j]))

                    {

                        tb.Lines[j].Remove(0);

                        chongfu++;

                    }

                }

 

            }

            MessageBox.Show("一共有" + tb.Lines.Length + "个原始数据\r\n去除了" + chongfu + "个重复数据\r\n" + "得到" + (tb.Lines.Length - chongfu) + "个不重复数据");

 

        //延时执行

        private void Delay(int mm)

        {

            DateTime current = DateTime.Now;

 

            while (current.AddMilliseconds(mm) > DateTime.Now)

            {

                Application.DoEvents();

            }

            return;

        }


修正版

        private void button1_Click(object sender, EventArgs e)

        {

            string content=quchongfu(textBox1);

            int hangshu = textBox1.Lines.Length;

            textBox2.Text = content;

            MessageBox.Show("一共有" + (hangshu - kongbaihang) + "个原始数据\r\n去除了" + chongfu + "个重复数据\r\n消除了"+kongbaihang+"个空白行\r\n得到" + (hangshu - kongbaihang - chongfu) + "个不重复数据");

 

        }

        //去除重复项

        int chongfu;

        int kongbaihang;

        private string quchongfu(TextBox tb)//textBox控件名称

        {

            chongfu = 0;

            kongbaihang = 0;

            string content = "";

            for (int i = tb.Lines.Length - 1; i >= 0; i--)

            {

                bool jia = true;//判断是否要加上

                //判断空白行

                if (tb.Lines[i] == "")

                {

                    kongbaihang++;

                    jia = false;

                }

 

                if (tb.Lines[i] != "")

                {

                    for (int j = i - 1; j >= 0; j--)

                    {

                        Delay(1);//延时执行,单位毫秒

                        if (tb.Lines[i].Equals(tb.Lines[j]))

                        {

                            chongfu++;

                            jia = false;

                        }

 

                    }

                    if (jia == true)

                    {

                        content = tb.Lines[i] + "\r\n" + content;

                    }

 

 

                }

            }

            return content;

        }

        #region 延时,方法

        //延时执行

        private void Delay(int mm)

        {

            DateTime current = DateTime.Now;

 

            while (current.AddMilliseconds(mm) > DateTime.Now)

            {

                Application.DoEvents();

            }

            return;

        }

        #endregion

 

最终版

        private void button1_Click(object sender, EventArgs e)

        {

            string content=quchongfu(textBox1);

            int hangshu1 = textBox1.Lines.Length;

            int hangshu2 = textBox2.Lines.Length;

            textBox2.Text = content;

            MessageBox.Show("一共有" + (hangshu1 - kongbaihang) + "个原始数据\r\n去除了" + chongfu + "个重复数据\r\n消除了"+kongbaihang+"个空白行\r\n得到" + (hangshu1 - kongbaihang - chongfu) + "个不重复数据");

 

        }

        //去除重复项

        int chongfu;

        int kongbaihang;

        private string quchongfu(TextBox tb)//textBox控件名称

        {

            chongfu = 0;

            kongbaihang = 0;

            string content = "";

            for (int i = 0; i <tb.Lines.Length; i++)

            {

                bool jia = true;//判断是否要加上

                //判断空白行

                if (tb.Lines[i] == "")

                {

                    kongbaihang++;

                    jia = false;

                }

 

                if (tb.Lines[i] != "")

                {

                    for (int j = 0; j < i;j++ )

                    {

                        Delay(1);//延时执行,单位毫秒

                        if (tb.Lines[i].Equals(tb.Lines[j]))

                        {

                            chongfu++;

                            jia = false;

                        }

 

                    }

                    if (jia == true)

                    {

                        content = content + tb.Lines[i] + "\r\n";

                    }

 

 

                }

            }

            return content;

        }

        #region 延时,方法

        //延时执行

        private void Delay(int mm)

        {

            DateTime current = DateTime.Now;

 

            while (current.AddMilliseconds(mm) > DateTime.Now)

            {

                Application.DoEvents();

            }

            return;

        }

        #endregion



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有