How to count the digits from a string using Linq in C#

1 Answer

0 votes
using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        string s = "ABC75DE8FG-J80-900C-21PO";

        var result = from ch in s
                     where char.IsDigit(ch)
                     select ch;

        int count = result.Count();
             
        Console.WriteLine(count);
    }
}




/*
run:

10

*/

 



answered Jul 13, 2023 by avibootz

Related questions

1 answer 130 views
1 answer 215 views
1 answer 138 views
1 answer 161 views
1 answer 147 views
1 answer 145 views
...