如何指示字符串是否具有数值 [英] How to indicate if string has numeric value
问题描述
我循环遍历字符串变量 data
,它可能具有整数数值,例如 "123"
.如果这个字符串变量有数值,我想指出它并考虑使用这样的:
I loop over string variable data
which may have integer numeric value, like "123"
. If this string variable has numeric value, I want to indicate it and thought to use some like this:
If IsNumeric(CInt(data)) Then
WScript.Echo "Number"
Else
WScript.Echo "String"
End If
但是 CInt()
每次 data
变量无法转换为整数时都会引发错误:
But CInt()
raises error every time data
variable can't be converted to integer:
类型不匹配:'CInt'
如何在 vbscript 中指示字符串是否具有整数值?
How can I indicate if string has integer value in vbscript?
推荐答案
IsNumeric vb 脚本的函数可用于判断表达式是否可以计算为数字.它根据表达式返回布尔值
IsNumeric Function of vb script can be used to determine whether an expression can be evaluated as a number.It returns Boolean value depending upon expression
请注意,如果表达式是日期表达式,则 IsNumeric 返回 False.
Please be noted that IsNumeric returns False if expression is a date expression.
现在,在您的代码中,您误认为即使它不是数字,您也试图将其转换为整数
Now, in your code you have mistaken that even if it is not number, you trying to convert it into integer
您可以像这样在代码中使用它--
You can use this in your code like this--
If IsNumeric(data) Then
WScript.Echo "Number"
Else
WScript.Echo "String"
End If
这篇关于如何指示字符串是否具有数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!