[英]If textBox1 Contains Integer
I just have a simple question... How do I check to see if a textbox or a string contains an Integer? 我只是有一个简单的问题...如何检查文本框或字符串是否包含整数?
please no code just maybe a hint or two :D 请没有代码只是一个提示或两个:D
thanks all :) 谢谢大家:)
hint 1: have a look on the static methods of int... there are 2 methods 提示1:看一下int的静态方法...有2种方法
hint 2: try regex 提示2:尝试正则表达式
int.TryParse(....
使用正则表达式模式。
提示:Int32中有一个方法,如果传递的对象不是整数,则该方法返回false。
use this regex pattern to validate if the text contains numbers only: 使用此正则表达式模式来验证文本是否仅包含数字:
^[0-9]+$ ^ [0-9] + $
when invalid, means that there is non numeric chars. 无效时,表示存在非数字字符。
Regex regex = new Regex("^[0-9]+$"); 正则表达式regex = new Regex(“ ^ [0-9] + $”);
regex.IsMatch(textbox1.Text); regex.IsMatch(textbox1.Text);
use regular expressions to check if the string contains an integer : 使用正则表达式检查字符串是否包含整数:
if (Regex.IsMatch(yourString, "\\d"))
{
// Do your stuff
}
A hint - The value in the textox is a string, try to parse it to int and if exception is raised - it is not an integer 提示-textox中的值是一个字符串,请尝试将其解析为int,如果引发异常-它不是整数
EDIT: Actually there is a method which does that - Int32.TryParse 编辑:其实有一种方法可以做到这一点-Int32.TryParse
you can try int.TryParse
or LINQ. 您可以尝试
int.TryParse
或LINQ。 The preferable and probably cleanest solution would be a RegEx, though. 不过,最好且可能最干净的解决方案是RegEx。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.