C#如何判断一个字符串中是否有非英文、数字字符

2025-02-22 11:54:03
推荐回答(2个)
回答1:

using System.Text.RegularExpressions;

Regex rex = new Regex("[a-z0-9A-Z_]+");
Match ma = rex.Match("你的字符串");
if (ma.Success)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("Fail");
}

回答2:

string str = "abcdefg123_";
foreach(char s in str)
{
if (!char.IsLetter(s))
{
//不是字母的情况
}
}