In C#, how to check whether a string contains an integer?
Solution 1:
You could use char.IsDigit:
bool isIntString = "your string".All(char.IsDigit)
Will return true
if the string is a number
bool containsInt = "your string".Any(char.IsDigit)
Will return true
if the string contains a digit
Solution 2:
Assuming you want to check that all characters in the string are digits, you could use the Enumerable.All Extension Method with the Char.IsDigit Method as follows:
bool allCharactersInStringAreDigits = myStringVariable.All(char.IsDigit);
Solution 3:
Maybe this can help
string input = "hello123world";
bool isDigitPresent = input.Any(c => char.IsDigit(c));
answer from msdn.