How to group words in a string by the first N letters in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic
Imports System.Text.RegularExpressions

Module Module1

    Function GroupByFirstNLetters(s As String, Optional n As Integer = 3) _
        As Dictionary(Of String, List(Of String))

        Dim groups As New Dictionary(Of String, List(Of String))()
        Dim regex As New Regex("[A-Za-z]+")
        Dim matches = regex.Matches(s.ToLower())

        For Each m As Match In matches
            Dim word = m.Value
            If word.Length >= n Then
                Dim prefix = word.Substring(0, n)

                If Not groups.ContainsKey(prefix) Then
                    groups(prefix) = New List(Of String)()
                End If

                groups(prefix).Add(word)
            End If
        Next

        Return groups
    End Function

    Sub Main()
        Dim s As String =
            "The lowly inhabitants of the lowland were surprised to see " &
            "the lower branches of the trees."

        Dim groups = GroupByFirstNLetters(s, 3)

        ' First print style
        For Each kv In groups
            Console.WriteLine($"{kv.Key} : [{String.Join(", ", kv.Value)}]")
        Next

        Console.WriteLine()

        ' Second print style
        For Each kv In groups
            Console.WriteLine($"{kv.Key}: {String.Join(", ", kv.Value)}")
        Next
    End Sub

End Module


' run:
' 
' the : [the, the, the, the]
' low : [lowly, lowland, lower]
' inh : [inhabitants]
' wer : [were]
' sur : [surprised]
' see : [see]
' bra : [branches]
' tre : [trees]
' 
' the: the, the, the, the
' low: lowly, lowland, lower
' inh: inhabitants
' wer: were
' sur: surprised
' see: see
' bra: branches
' tre: trees
' 

 



answered Mar 13 by avibootz
...