int.TryParse与其他用于确定char是否包含int的方法 [英] int.TryParse vs. other methods for determining if a char contains an int

查看:117
本文介绍了int.TryParse与其他用于确定char是否包含int的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用char数据类型时,出于任何原因应该使用int.TryParse

When using the char datatype is there any reason one should use int.TryParse

int.TryParse(inputChar.ToString(), NumberStyles.Integer, 
                             CultureInfo.InvariantCulture, out curNum)

vs.

inputChar - '0'

并检查结果是否在0-9之间?

And checking if the result is between 0-9?

推荐答案

如果要检查char是否为数字,则应使用

If you want to check if a char is a digit you should use Char.IsDigit:

if (Char.IsDigit(inputChar))
{ 
    // ...
}

这篇关于int.TryParse与其他用于确定char是否包含int的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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