Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

c# - 识别字符串是否为数字(Identify if a string is a number)

If I have these strings:

(如果我有这些字符串:)

  1. "abc" = false

    ("abc" = false)

  2. "123" = true

    ("123" = true)

  3. "ab2" = false

    ("ab2" = false)

Is there a command, like IsNumeric() or something else, that can identify if a string is a valid number?

(是否存在诸如IsNumeric()类的命令,可以识别字符串是否为有效数字?)

  ask by Gold translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
int n;
bool isNumeric = int.TryParse("123", out n);

Update As of C# 7:

(从C#7开始更新 :)

var isNumeric = int.TryParse("123", out int n);

The var s can be replaced by their respective types!

(var可以用它们各自的类型替换!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...