本文介绍了识别字符串是否为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有以下字符串:
-
"abc"
=false
"123"
= true
"ab2"
= false
是否存在诸如IsNumeric()
之类的命令,可以识别字符串是否为有效数字?
Is there a command, like IsNumeric()
or something else, that can identify if a string is a valid number?
推荐答案
int n;
bool isNumeric = int.TryParse("123", out n);
更新,从C#7开始:
var isNumeric = int.TryParse("123", out int n);
或者如果您不需要该号码,则可以丢弃 out参数
or if you don't need the number you can discard the out parameter
var isNumeric = int.TryParse("123", out _);
var 可以用它们各自的类型替换!
The var s can be replaced by their respective types!
这篇关于识别字符串是否为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!