Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 271
OneFreeFitz Avatar asked May 12 '11 20:05

OneFreeFitz


People also ask

How do you count numbers in a string?

isDigit(s. charAt(i)) will count the number of digits in the given string. 'Character. isDigit' will count the number of each digit in the string.

Can you use Countif with text?

The COUNTIF function returns incorrect results when you use it to match strings longer than 255 characters. To match strings longer than 255 characters, use the CONCATENATE function or the concatenate operator &. For example, =COUNTIF(A2:A5,"long string"&"another long string").


1 Answers

Using LINQ :

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

or

var count = jobId.Count(Char.IsDigit);
like image 65
mathieu Avatar answered Sep 24 '22 01:09

mathieu