我如何检查给定的字符串是否为整数 [英] How can i check the given string is integer or not

查看:52
本文介绍了我如何检查给定的字符串是否为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在获取给定格式的字符串值

Hi i am getting a string value in the given format

1)abc<br />
2)abc23232<br />
3)1,23232<br />
4)1221<br />
5)-32323



现在如何检查
1)字符串是否具有整数值和
2)如果它具有整数值,我如何检查它是否为负值



now how can i check
1)whether the string has integer value and
2)if it has interger value how i can check whether it is negative or not

推荐答案

使用它..
Int32.Parse [
use this..
Int32.Parse[^]


(1)存在Int32.TryParse [
(1) There''s the Int32.TryParse[^] method for that (please read the documentation).
(2) Once TryParse returns true you have the integer and you may test if it is less than zero.


尝试一下,

Try this,

int checkNumber;
if (int.TryParse(TextBox2.Text, out checkNumber) == false)
{
    //Entered number is not interger;
}
else
{
    if (Convert.ToInt16(TextBox2.Text) < 0)
    {
        //Entered no. is negative
    }
    else
    {
        //Entered no. is positive
    }
}


这篇关于我如何检查给定的字符串是否为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆