Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

如何从指定字符串中查找并替换字符串?

$
0
0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#region              吴新强于2013年4月10日0:46:02 桂电2507
#endregion
namespace Find
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string str = "";
            richTextBox1.Text = str;
        }
        int start = 0;
        int count = 0;
        /// <summary>
        /// 查找字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void find_Click(object sender, EventArgs e)
        {
            string str1;
            str1 = txt_find.Text;
            if (start >= richTextBox1.Text.Length)
            {
                MessageBox.Show("以查找到尾部");
                start = 0;
            }
            else
            {
                start = richTextBox1.Find(str1, start, RichTextBoxFinds.MatchCase);
                if (start == -1)
                {
                    if (count == 0)
                    {
                        MessageBox.Show("没有该字符!");
                    }
                    else
                    {
                        MessageBox.Show("以查找到尾部!");
                        start = 0;
                    }
                }
                else
                {
                    start = start + str1.Length;
                    richTextBox1.Focus();
                }
            }
        }
        /// <summary>
        /// 替换字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replace_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = richTextBox1.Text.Replace(txt_find.Text, txt_replace.Text);
        }
        /// <summary>
        /// 输入查找的字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_find_TextChanged(object sender, EventArgs e)
        {
            string str1;
            str1 = txt_find.Text;
            start = 0;
            count = 0;
        }

    }
}

实验结果:

 

作者:smart819819 发表于2013-4-10 0:46:17 原文链接
阅读:129 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>