如果检查号码的条件如何申请? [英] How to apply if condition for checking number?

查看:46
本文介绍了如果检查号码的条件如何申请?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想应用if条件来检查结果值是否有数字

之类,以下语句有字符串值result从服务器返回,我想检查一下,这个result字符串包含数字(0-9)与否,temp是条形码字符串。



i试用了以下内容,但这给出了错误,现在该做什么,





I want to apply " if " condition for checking resulting values has numbers or not
like, the following statement has string value " result " is returning from server and I want to check that, this " result " string contains numbers ( 0-9 ) or not and temp is the barcode string.

i tried with the following , But this gives error, what to do now,


string result = System.Text.Encoding.ASCII.GetString(temp);
int [ ]a ={ 0,1,2,3,4,5,6,7,8,9 } 
if (result.contains(a) == true )
{}

推荐答案

阅读: http://stackoverflow.com/questions/1540620/check-if-a-string-has-at-least-one-number-in-it-using-linq [ ^ ]


完全取决于什么你在干嘛通常,这种检查是为了确保字符串是一个数字,而不是包含至少一个数字。

您可以通过查看数字进行检查......但我不会t。

我这样做:

Depends exactly what you are doing. Normally, this kind of check is to make sure that the string is a number, rather than "contains at least one digit".
You can check by looking at the digits...but I wouldn't.
I'd do this:
string result = System.Text.Encoding.ASCII.GetString(temp);
int value;
if (int.TryParse(result, out value))
   {
   ...
   }



这样,你确认它是一个有效数字(可以包括一个前导减号)并获得值,以便以后使用,如果你需要它......你可能会这样做。


That way, you confirm that it's a valid number (which can include a leading minus sign) and get the value for later use if you need it...which you probably do.


这篇关于如果检查号码的条件如何申请?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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