Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

You could use int.TryParse. It will return true if the supplied string represents a valid number, false otherwise. Don't forget to use NumerStyles.None to disallow blank spaces and the plus/minus sign.

UPDATE: As empi says, this will not work for very large strings. If you have arbitrarily large strings, maybe a regex is your only option. You could do something like Regex.IsMatch(theString,"^[0-9]+$")

bool onlyDigits = "1234".All(c => char.IsDigit(c));

Methinks, you'd be better to use regex to do the job for you... "\d+" should do it...ok, you said you do not want to use it... but it is foolproof way of ensuring the string contains only numbers, then if the regexp passes, then you can use int.Parse(...) straightaway.

Yes, you can use

bool Int32.TryParse(string s, out int result);

Code sample:

string myString = "1265";
int myInt;

if (Int32.TryParse(myString,myInt) // Returns true if mystring contains only digits
{
...
}

Other option is to use Regex:

    public static bool IsDigit(string myString)
    {
        string pattern = @"^\d*$";

        if (Regex.IsMatch(myString, pattern))
            return true;
        else
            return false;
    }

You can change the pattern as per your requirement.

There is no built-in method. Int32.TryParse or Int64.TryParse won't work for very long strings containing just numbers (they may also allow other chars used to represent integer numbers).

As other people have said, you can use the int.TryParse method, although unless your number is really really EXTREMELY big, you could use the TryParse method of other types, that have a bigger range than int32's. (of course using the NumberStyles.None option, to avoid signs and punctuation).

Here's the break down:

  1. int -2,147,483,648 .. 2,147,483,647
  2. uint 0 .. 4,294,967,295
  3. long -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
  4. ulong 0 .. 18,446,744,073,709,551,615
  5. float -3.402823e38 .. 3.402823e38
  6. double -1.79769313486232e308 .. 1.79769313486232e308
  7. decimal -79228162514264337593543950335 .. 79228162514264337593543950335

The one that can parse the biggest numbers is Double. If you need to use the number , you will lose some precision, but it can parse really long numbers (although you say you dont need to use it so it shouldnt be a problem). In a quick test I did, it managed to successfully parse the following string:

79228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335792281625142643375935439503357922816251426433759354395033579228162514264337593543950335234234234234243423

(thats 308 characters, it would fail with one more number)

Still, if you are not going to use the number, it might be an overkill, so I would go for the Regex, or even better the loop checking that each character is a digit.

If you then wanna go a little crazy you could split that into several smaller strings, and use the Task library to check it in a parallel way :P

(I know its a little offtopic now, but if you DO want to do that, you should check parallel.for and the Partition Ranger, check out this C9 10min clip: Parallel For Partition Ranger )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top