检查字符串变量是否具有整数值 [英] Check if a string variable has an integer value

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

问题描述

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

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.

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

谢谢,
Kai:)

Thanks, Kai :)

推荐答案

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

A very simple trick is to try parse the 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

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

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