How to count the number of numeric digits in a string with C#

1 Answer

0 votes
using System;
using System.Linq; 

class Program
{
    static void Main() {
        string str = "ABCD4E872F-F35-190-91A";

        var queryResult = from ch in str
                          where Char.IsDigit(ch)
                          select ch;
        
        int count = queryResult.Count();
        
        Console.WriteLine(count);
    }
}
 
 
 
/*
run:
 
11
 
*/

 



answered May 1, 2024 by avibootz
...