isnumeric

Identify if a string is a number

Deadly 提交于 2019-11-25 22:57:41
问题 If I have these strings: \"abc\" = false \"123\" = true \"ab2\" = false Is there a command, like IsNumeric() or something else, that can identify if a string is a valid number? 回答1: int n; bool isNumeric = int.TryParse("123", out n); Update As of C# 7: var isNumeric = int.TryParse("123", out int n); The var s can be replaced by their respective types! 回答2: This will return true if input is all numbers. Don't know if it's any better than TryParse , but it will work. Regex.IsMatch(input, @"^\d+