textBox1中是字符串例如“adsadasdasdnm”
textBox2中是要找的字符串例如“sa”
int index = 0;
private void button2_Click(object sender, EventArgs e)
{
index = textBox1.Text.IndexOf(textBox2.Text, index);
if (index < 0)
{
index = 0;
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;
MessageBox.Show("已到结尾");
return;
}
textBox1.SelectionStart = index;
textBox1.SelectionLength = textBox2.Text.Length;
index = index + textBox2.Text.Length;
textBox1.Focus();
}
regex是正则表达式。网上应该有很多已经很好的表达式。你只需要动态替换其中的关键字就可以了