gpt4 book ai didi

c# - 如何检查字符串不包含字母和数字以外的任何字符?

转载 作者:行者123 更新时间:2023-11-30 13:31:54 24 4
gpt4 key购买 nike

这是我目前所拥有的,但我找不到任何代码来说明我只想包含字母和数字。我不熟悉正则表达式。现在,即使我包含“#”,我的代码也会忽略 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com