29

How I can check if my string only contain numbers?

I don't remember. Something like isnumeric?

0

7 Answers 7

83

Just check each character.

bool IsAllDigits(string s)
{
    foreach (char c in s)
    {
        if (!char.IsDigit(c))
            return false;
    }
    return true;
}

Or use LINQ.

bool IsAllDigits(string s) => s.All(char.IsDigit);

If you want to know whether or not a value entered into your program represents a valid integer value (in the range of int), you can use TryParse(). Note that this approach is not the same as checking if the string contains only digits.

bool IsAllDigits(string s) => int.TryParse(s, out int i);
Sign up to request clarification or add additional context in comments.

Comments

11

You could use Regex or int.TryParse.

See also C# Equivalent of VB's IsNumeric()

5 Comments

+1 Although I disagree with the article's recommendation to avoid using library functions offered in the Microsoft.VisualBasic namespace. It's not legacy code or anything like that. If it does what you want more easily than the other solutions, use it.
I wouldn't suggest using int.TryParse or long.TryParse, since they can overflow.
@JimMischel: In the current version of .NET, int.TryParse() does not throw overflow exceptions. It just returns false.
@JonathanWood: You misunderstood my use of the term "overflow" here. My point was that int.TryParse would return false for the value 9876543210, due to overflow. That is, the value "9,876,543,210" is too large to fit in an int.
@JimMischel: Ok, so you're saying it should return true in that case because all the characters are, in fact, digits. So, technically, that is correct given the question, and my (accepted) answer deals with that too. But, in practice, I suspect most people wanting this information do, in fact, plan to assign the value to an integer. TryParse() would be helpful in that case.
8

int.TryParse() method will return false for non numeric strings

2 Comments

It will also throw an overflow exception if the string is too long.
At least in the current version of .NET, int.TryParse() does not throw an exception if the string is too long or the integer is too big.
8

Your question is not clear. Is . allowed in the string? Is ¼ allowed?

string source = GetTheString();

//only 0-9 allowed in the string, which almost equals to int.TryParse
bool allDigits = source.All(char.IsDigit); 
bool alternative = int.TryParse(source,out result);

//allow other "numbers" like ¼
bool allNumbers = source.All(char.IsNumber);

Comments

5

If you want to use Regex you would have to use something like this:

string regExPattern = @"^[0-9]+$";
System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex(regExPattern);
return pattern.IsMatch(yourString);

Comments

4
public bool IsNumeric(string str)
{
    return str.All(c => "0123456789".Contains(c);
}

Comments

0

You con do like that:

public bool IsNumeric(string val)
{
    if(int.TryParse(val)) return true;
    else if(double.TryParse(val)) return true;
    else if(float.TryParse(val)) return true;
    else return false;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.