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:
int
-2,147,483,648 .. 2,147,483,647
uint
0 .. 4,294,967,295
long
-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
ulong
0 .. 18,446,744,073,709,551,615
float
-3.402823e38 .. 3.402823e38
double
-1.79769313486232e308 .. 1.79769313486232e308
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 )