怎么检测文本框里有没单引号

来源:百度知道 编辑:UC知道 时间:2024/05/31 15:48:35
如题

用String对象的Contains方法
string s = "asdf'";
s.Contains("'");//返回的是bool值,这个例子应该是true

用查找单引号索引位置的方法,如果i==-1,则此字符串没有单号,如果不为-1,则输出单引号在字符中的位置,由于是索引值,所以输出时需要进行i+1的操作!

int i = this.textBox1.Text.LastIndexOf("'");
if (i == -1)
{
MessageBox.Show("没有单引号");
}
else
{
MessageBox.Show("有单引号,位置为:第" + (i+1)+"个字符");
}