How to count repeated words in a string and find its position?

0

Imagine the Alphabet, in this alphabet you have words from A-Z, but suddenly there are several letters A, such as:

A -BCDEFGHI- A -JKL- A -M- A -NOPQ- A

The variable nomes below, is this alphabet. The variable procurar will only serve for me to choose which repeated word I want to see in the nomes variable.

In this template:

string procurar = "cachorro";
string nomes = "vaca cachorro vaca cachorro gato cavalo";
int index = ??
int total = ??

Can anyone help?

    
asked by anonymous 21.01.2018 / 21:24

1 answer

2

See if it helps:

string procurar = "cachorro";
string nomes = "vaca cachorro vaca cachorro gato cavalo";
MatchCollection matches = Regex.Matches(nomes, procurar);
foreach (Match item in matches)
{
    Console.WriteLine(string.Format("{0} = {1}", procurar, item.Index));
}
Console.WriteLine(string.Format("achado {0} nomes ", matches.Count));
    
21.01.2018 / 21:35