Como: Ocorrências de contagem de uma palavra em uma seqüência de caracteres (LINQ)

Este exemplo mostra como usar um LINQ consulta para contar as ocorrências de uma palavra especificada em uma seqüência de caracteres. Observe que para realizar a contagem, primeiro o Split método é chamado para criar uma matriz de palavras. Há um custo de desempenho para o Split método. Se a operação apenas na seqüência de caracteres contar as palavras, você deve considerar o uso de Matches ou IndexOf métodos em vez disso. No entanto, se o desempenho não é um problema crítico ou se você já tenha dividido a sentença para executar outros tipos de consultas sobre ele, em seguida, faz sentido usar LINQ para contar as palavras ou frases como bem.

Exemplo

Class CountWords

    Shared Sub Main()

        Dim text As String = "Historically, the world of data and the world of objects" & 
                  " have not been well integrated. Programmers work in C# or Visual Basic" & 
                  " and also in SQL or XQuery. On the one side are concepts such as classes," & 
                  " objects, fields, inheritance, and .NET Framework APIs. On the other side" & 
                  " are tables, columns, rows, nodes, and separate languages for dealing with" & 
                  " them. Data types often require translation between the two worlds; there are" & 
                  " different standard functions. Because the object world has no notion of query, a" & 
                  " query can only be represented as a string without compile-time type checking or" & 
                  " IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to" & 
                  " objects in memory is often tedious and error-prone."

        Dim searchTerm As String = "data"

        ' Convert the string into an array of words.
        Dim dataSource As String() = text.Split(New Char() {" ", ",", ".", ";", ":"}, 
                                                 StringSplitOptions.RemoveEmptyEntries)

        ' Create and execute the query. It executes immediately 
        ' because a singleton value is produced.
        ' Use ToLower to match "data" and "Data" 
        Dim matchQuery = From word In dataSource 
                      Where word.ToLowerInvariant() = searchTerm.ToLowerInvariant() 
                      Select word

        ' Count the matches.
        Dim count As Integer = matchQuery.Count()
        Console.WriteLine(count & " occurrence(s) of the search term """ & 
                          searchTerm & """ were found.")

        ' Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.")
        Console.ReadKey()
    End Sub
End Class
' Output:
' 3 occurrence(s) of the search term "data" were found.
class CountWords
{
    static void Main()
    {
        string text = @"Historically, the world of data and the world of objects" +
          @" have not been well integrated. Programmers work in C# or Visual Basic" +
          @" and also in SQL or XQuery. On the one side are concepts such as classes," +
          @" objects, fields, inheritance, and .NET Framework APIs. On the other side" +
          @" are tables, columns, rows, nodes, and separate languages for dealing with" +
          @" them. Data types often require translation between the two worlds; there are" +
          @" different standard functions. Because the object world has no notion of query, a" +
          @" query can only be represented as a string without compile-time type checking or" +
          @" IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to" +
          @" objects in memory is often tedious and error-prone.";

        string searchTerm = "data";

        //Convert the string into an array of words
        string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);

        // Create and execute the query. It executes immediately 
        // because a singleton value is produced.
        // Use ToLowerInvariant to match "data" and "Data" 
        var matchQuery = from word in source
                         where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
                         select word;

        // Count the matches.
        int wordCount = matchQuery.Count();
        Console.WriteLine("{0} occurrences(s) of the search term \"{1}\" were found.", wordCount, searchTerm);

        // Keep console window open in debug mode
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
}
/* Output:
   3 occurrences(s) of the search term "data" were found.
*/

Compilando o código

  • Criar um Visual Studio o projeto que se destina a .NET Framework versão 3.5. Por padrão, o projeto tem uma referência a System.Core.dll e um using diretiva (C#) ou Imports instrução (Visual Basic) para o namespace System. LINQ. No C# projetos, adicione um using a diretiva para o namespace System. IO.

  • Copie este código em seu projeto.

  • Pressione F5 para compilar e executar o programa.

  • Pressione qualquer tecla para sair da janela do console.

Consulte também

Conceitos

LINQ e seqüências de caracteres