这是我目前所拥有的,但我找不到任何代码来说明我只想包含字母和数字。我不熟悉正则表达式。现在,即使我包含“#”,我的代码也会忽略 while 循环。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void okBtn_Click(object sender, EventArgs e)
{
if(textBox1.Text.Contains(@"^[^\W_]*$"))
{
fm1.txtFileName = textBox1.Text;
this.Close();
}
else
{
MessageBox.Show("Filename cannot include illegal characters.");
}
}
}
最佳答案
您可以使用方法char.IsLetterOrDigit
检查输入字符串是否仅包含字母或数字:
if (input.All(char.IsLetterOrDigit))
{
//Only contains letters and digits
...
}
关于c# - 如何检查字符串不包含字母和数字以外的任何字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17839967/