我有一个字符串数组
string [] ar = { "net" , "com" , "org"};
Run Code Online (Sandbox Code Playgroud)
当用户输入网站示例以数组中的字符串结尾时,我希望标签写为"true"
我试过这个:
private void timer1_Tick(object sender, EventArgs e)
{
foreach ( string ex in ar)
{
if (Regex.IsMatch(txtbox.Text, @"^(www\.)([\w]+)\.(" + ex + ")$"))
{
lbl.Text = "True";
}
else
{
lbl.Text = "False";
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我写"www.google.com"时,标签仍在写false.只有当我写"www.google.org"时,标签才会写道true.
foreach在第一场比赛结束后,您需要立即停止迭代.或者,更好的是,
lbl.Text = ar.Any(s => Regex.IsMatch(txtbox.Text, "..." + s + "...")) ? "True" : "False"
Run Code Online (Sandbox Code Playgroud)
另外,在WinForms应用程序中要小心计时器:你只能安全地使用System.Windows.Forms.Timer,而不是System.Threading.Timer.
| 归档时间: |
|
| 查看次数: |
1192 次 |
| 最近记录: |