[英]Windows forms textbox or c# string native method for checking non-alphanumeric character
Is there a windows form textbox or c# string native member method that checks if its contents have any non-alphanumeric character? 是否有一个Windows窗体文本框或c#字符串本机成员方法,检查其内容是否有任何非字母数字字符?
or do I have to do it manually? 或者我必须手动完成吗?
EDIT: I used @Habib's answer and added so that white spaces are checked as well, and to my surprise, it worked! 编辑:我使用了@Habib的答案并添加了以便检查空格,令我惊讶的是,它有效! lol
大声笑
bool result = strVariable.Any(r=> (!char.IsLetterOrDigit(r) && !char.IsWhiteSpace(r)));
Btw, I've never used the "lambda" expression that's why I'm surprised the code above worked when I added the whitespace condition on @Habib's initial answer. 顺便说一句,我从未使用过“lambda”表达式,这就是为什么当我在@Habib的初始答案中添加空白条件时,我对上面的代码感到惊讶的原因。
You can use char.IsLetterOrDigit 您可以使用char.IsLetterOrDigit
Indicates whether a Unicode character is categorized as a letter or a decimal digit.
指示Unicode字符是分类为字母还是十进制数字。
bool result = strVariable.Any(r=> !char.IsLetterOrDigit(r));
您可以创建一个以TextBox为基础的控件,但是目前没有为您执行此操作的属性,您必须在textBox的KeyUp事件上执行此操作并使用正则表达式或类似的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.