How to count close repeating characters of a string in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main() {
        String s = "c# wwwww programming ooo w"; 
       
        var matches = Regex.Matches(s, @"(.)\1+");
        for (int i = 0; i < matches.Count; i++) {
            Console.WriteLine(matches[i].Value + " : " + matches[i].Length + " times");

        }     
    }
}




/*
run:

wwwww : 5 times
mm : 2 times
ooo : 3 times

*/

 



answered Jan 11, 2020 by avibootz

Related questions

1 answer 124 views
1 answer 136 views
1 answer 169 views
1 answer 110 views
...