Question

How can I count the numbers in a string of mixed text/numbers

So what I'm trying to do, is take a job number, which looks like this xxx123432, and count the digits in the entry, but not the letters. I want to then assign the number of numbers to a variable, and use that variable to provide a check against the job numbers to determine if they are in a valid format.

I've already figured out how to perform the check, but I have no clue how to go about counting the numbers in the job number.

Thanks so much for your help.

 21  31386  21
1 Jan 1970

Solution

 47

Using LINQ :

var count = jobId.Count(x => Char.IsDigit(x));

or

var count = jobId.Count(Char.IsDigit);
2011-05-12

Solution

 4
int x = "xxx123432".Count(c => Char.IsNumber(c)); // 6

or

int x = "xxx123432".Count(c => Char.IsDigit(c)); // 6

The difference between these two methods see at Char.IsDigit and Char.IsNumber.

2011-05-12