28

How I can check if my string only contain numbers?

I don't remember. Something like isnumeric?

Gold
  • 60,526
  • 100
  • 215
  • 315
  • 1
    Possible duplicate of [How do I identify if a string is a number?](http://stackoverflow.com/questions/894263/how-do-i-identify-if-a-string-is-a-number) – Jim Fell Jun 17 '16 at 16:51

7 Answers7

82

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 numbers.

bool IsAllDigits(string s) => int.TryParse(s, out int i);
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
11

You could use Regex or int.TryParse.

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

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
  • +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. – Cody Gray - on strike Dec 24 '10 at 07:57
  • 4
    I wouldn't suggest using `int.TryParse` or `long.TryParse`, since they can overflow. – Jim Mischel Dec 24 '10 at 09:07
  • @JimMischel: In the current version of .NET, `int.TryParse()` does not throw overflow exceptions. It just returns false. – Jonathan Wood Sep 05 '14 at 14:54
  • @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`. – Jim Mischel Sep 05 '14 at 15:13
  • 1
    @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. – Jonathan Wood Sep 05 '14 at 15:39
8

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

TalentTuner
  • 17,262
  • 5
  • 38
  • 63
7

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);
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
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);
Nikos Steiakakis
  • 5,605
  • 7
  • 44
  • 54
4
public bool IsNumeric(string str)
{
    return str.All(c => "0123456789".Contains(c);
}
H.M.
  • 612
  • 1
  • 5
  • 7
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;
}
CharithJ
  • 46,289
  • 20
  • 116
  • 131
RAMe0
  • 1,415
  • 2
  • 19
  • 31