string 检查字符串变量是否具有整数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13980538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:45:54  来源:igfitidea点击:

Check if a string variable has an integer value

vb.netstringvisual-studio-2010parsinginteger

提问by Kaimund600

I am working on a project which allows kids to send a message to Santa. Unfortunately, if they enter a string instead of an integer in the AGE field, the program crashes and returns Conversion from string "[exampleString]" to type 'Double' is not valid. Is there any way to check if they have entered an integer or not? This is the code.

我正在开展一个项目,该项目允许孩子们向圣诞老人发送信息。不幸的是,如果他们在 AGE 字段中输入字符串而不是整数,程序将崩溃并返回 Conversion from string "[exampleString]" to type 'Double' is not valid。有没有办法检查他们是否输入了整数?这是代码。

If childAge > 0 And childAge < 150 Then
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
Else
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
End If

Thanks, Kai :)

谢谢,凯:)

回答by Styxxy

A very simple trick is to try parsethe string as an Integer. If it succeeds, it is an integer (surprise surprise).

一个非常简单的技巧是尝试将字符串解析为整数。如果成功,它是一个整数(惊喜惊喜)。

Dim childAgeAsInt As Integer
If Integer.TryParse(childAge, childAgeAsInt) Then
    ' childAge successfully parsed as Integer
Else
    ' childAge is not an Integer
End If

回答by rwells

You could perform the following two tests to be reasonably certain that the input you're getting is an integer:

您可以执行以下两个测试以合理确定您获得的输入是整数:

If IsNumeric(childAge) AndAlso (InStr(1, childAge, ".") <> 0) Then
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
    If childAge < 0 OrElse childAge > 150 Then
        fmSecA2 = "I don't believe it's possible to be" & childAge & " years old..."
    End If
Else
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"

The InStrfunction returns zero if it doesn't find the string that is being looked for, and so when combining that test with IsNumeric, you also rule out the possibility that some floating point data type was entered.

如果InStr函数未找到要查找的字符串,则它返回零,因此在将该测试与 IsNumeric 组合时,您还排除了输入某些浮点数据类型的可能性。

回答by Warchlak

Complementing Styxxy's response, if you dont need a result just replace it by vbNull:

补充 Styxxy 的响应,如果您不需要结果,只需将其替换为 vbNull:

If Integer.TryParse(childAge, vbNull) Then

回答by mmeasor

IsNumeric is built into VB, and will return a true/false

IsNumeric 内置于 VB 中,将返回真/假

If IsNumeric(childAge) AndAlso (childAge > 0 And childAge < 150) Then
    fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
Else
    fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
End If

回答by Quentin Perez

You can use this.

你可以用这个。

Sub checkInt() 
    If IsNumeric(Range("A1")) And Not IsEmpty(Range("A1")) Then 

        If Round(Range("A1"), 0) / 1 = Range("A1") Then 
            MsgBox "Integer: " & Range("A1") 
        Else 
            MsgBox "Not Integer: " & Range("A1") 
        End If 
    Else 
        MsgBox "Not numeric or empty" 
    End If 
End Sub 

回答by Kristian

Working from Styxxy's answer, if you parse as a byte rather than an integer, then it also checks negative ages and maximum age of 255 all in one go.

根据 Styxxy 的回答,如果您解析为一个字节而不是一个整数,那么它还会一次性检查负年龄和最大年龄 255。

Dim childAgeAsByte As Byte
If Byte.TryParse(childAge, childAgeAsByte) Then
    ' childAge successfully parsed as Byte
Else
    ' childAge is not a Byte
End If

Kristian

克里斯蒂安

回答by SAm

Dim Input 

 Input = TextBox1.Text
 If Input > 0 Then 
   ............................
   ............................
 Else 
   TextBox2.Text = "Please only enter positive integers"
 End If

回答by bonCodigo

In .Net you may use GetType()to determine the data type of a variable.

在 .Net 中,您可以GetType()用来确定变量的数据类型。

Dim n1 As Integer = 12
Dim n2 As Integer = 82
Dim n3 As Long = 12

Console.WriteLine("n1 and n2 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n2.GetType()))
Console.WriteLine("n1 and n3 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n3.GetType()))
' The example displays the following output: 
'       n1 and n2 are the same type: True 
'       n1 and n3 are the same type: False  

Based on the above sample you can write a code snippet:

基于上面的示例,您可以编写代码片段:

If childAge.GetType() = "Integer" then '-- also use childAge.GetType().Name = "Int32"
  ' do something
End if