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

时间:2012-12-20 21:13:09

标签: vb.net string visual-studio-2010 parsing integer

我正在开展一个项目,允许孩子们向圣诞老人发送信息。不幸的是,如果他们在AGE字段中输入字符串而不是整数,程序崩溃并返回从字符串“[exampleString]”到“Double”类型的转换无效。 有没有办法检查他们是否输入了整数?这是代码。

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

谢谢, 凯:)

9 个答案:

答案 0 :(得分:38)

一个非常简单的技巧是将try parse字符串作为整数。如果成功,则为整数(惊喜)。

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

答案 1 :(得分:4)

你可以执行以下两个测试,以合理地确定你得到的输入是一个整数:

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!"

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

答案 2 :(得分:2)

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

client_ip

答案 3 :(得分:1)

你可以使用它。

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 

答案 4 :(得分:1)

IsNumeric内置于VB中,并将返回true / false

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

答案 5 :(得分:0)

根据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

克里斯蒂安

答案 6 :(得分:0)

Dim Input 

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

答案 7 :(得分:0)

Try
    If TextBox1.Text > 0 Then
        Label1.Text = "Integer"
    End If
Catch ex As Exception
    Label1.Text = "String"
End Try

使用此方法,您可以在TextBox1中放入任何内容,如果您输入文本,则您得到Label1是字符串,如果您输入数字,则它是整数

答案 8 :(得分:-1)

在.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  

根据以上示例,您可以编写代码段:

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