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();
}
追问我想问下 index<0 是什么意思 - - 我刚找照那个打进去,查找是会查找的到,但是中间有几个字符串被跳过了 ,不是每一个都能搜索到,这是怎么回事
追答把 index = index + textBox2.Text.Length这句改成 index = index + 1吧,
index<0就是没找到的时候,这时应该返回起点,所以让index=0了。