I just want to know, whether a String variable contains a parsable positive integer value. I do NOT want to parse the value right now.
Currently I am doing:
int parsedId;
if (
(String.IsNullOrEmpty(myStringVariable) ||
(!uint.TryParse(myStringVariable, out parsedId))
)
{//..show error message}
This is ugly - How to be more concise?
Note: I know about extension methods, but I wonder if there is something built-in.
You can check if string contains numbers only:
But number can be bigger than
Int32.MaxValue
or less thanInt32.MinValue
- you should keep that in mind.Another option - create extension method and move ugly code there:
That will make your code more clean:
Maybe this can help
answer from msdn.
You could use char.IsDigit:
Will return
true
if the string is a numberWill return
true
if the string contains a digitSorry, didn't quite get your question. So something like this?
Or does the value have to be an integer completely, without any additional strings?
Assuming you want to check that all characters in the string are digits, you could use the Enumerable.All Extension Method with the Char.IsDigit Method as follows:
The answer seems to be just no.
Although there are many good other answers, they either just hide the uglyness (which I did not ask for) or introduce new problems (edge cases).