简体   繁体   English

如果textBox1包含整数

[英]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.

相关问题 将textbox1条目保存到xml中 - Save textbox1 entry into xml 如何从textbox1中选择textbox2? - How to select textbox2 from textbox1? document.getElementById('TextBox1')。value不起作用 - document.getElementById('TextBox1').value is not working 名称“ Textbox1”在当前上下文中不存在吗? - The name 'Textbox1' does not exist in the current context? 来自textbox1的用户输入>存储在textbox2中的列表>输出中 - User Input from textbox1 > store in a list > output in textbox2 客户端:如果textbox1和textbox 2设置了计数器++ - Client Side: if textbox1 and textbox 2 set counter++ 我有 2 个文本框,textbox1 有固定时间“HH:mm”,Textbox2 应该有 textbox1 时间加 10 分钟 - I have 2 textboxes , textbox1 has a fixed time " HH:mm", Textbox2 should have textbox1 time plus 10 minutes 当用户在 textbox1 和 textbox2 中键入无效字符时,会出现“无效字符消息”并且 textbox1 的文本变为红色 - When user types invalid characters in textbox1 and in textbox2 an "invalid character message" is appeared and the textbox1's text is made red 从textbox1获取文本,并以其他形式在textblock1上进行设置 - get text from textbox1 and set it on textblock1 in other form webbrowser表区域td如何在textbox1中抓取信息? - webbrowser table area td to How scraped information in textbox1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM