Search code examples
c#.netstringvalidationdigits

C#: Anything similar to the php ctype_digit function for C#?


I have a string and I want to check that it only consists of digits. I don't really want to (or need to) parse it or anything, I just want to know that it does not contain anything but digits (and no floating point separator either, just digits).

PHP has this nice function called ctype_digit. Is there anything similar for C# anywhere in the .Net Framework? Or do I need to use a regular expression or something like that?

Creating a regex for this would of course be pretty simple, but I would like to not use it if there are better ways :p


Solution

  • The answer of @bruno conde made me think of this :D

    if(subject.All(char.IsDigit))
        // Do something
    

    Can't believe I didn't think of it before...