-3

How to check the string is number or not. I am verifying mobile number codes in which it should have 10 digits and only in numerical format.

string str="9848768447"
if(str.Length==10 &&  Here I need condition to check string is number or not)
{
 //Code goes here
}

I am new to programming. please help me

1

1 Answer 1

6

Use int.TryParse:

int i;
if(str.Length==10 && int.TryParse(str, out i))
{
     //Code goes here
}

Another way which has issues with unicode digits is using Char.IsDigit:

if(str.Length==10 && str.All(Char.IsDigit))
{

}
Sign up to request clarification or add additional context in comments.

6 Comments

"which has issues with unicode digits" why even show it then D:
you could use regex as well Regex.IsMatch(input, @"^\d+$") or Regex.IsMatch(input, @"\d")
Wouldn't a RegEx solution be more efficient?
@EaterOfCode: because it can be useful, is efficient and the direct answer to OP's question. It is - by the way - similar to regex with the digit specifier but more efficient and readable.
@TimSchmelter not really, It's obvious (imo) that he is stating latin numbers and not arabic ones or other weird numeric characters. but hey whatever floats your boat, or integer from that perspective.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.