Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to count the occurrences of each word in a string with VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main()
		Dim str As String = "Visual Basic (VB), originally called Visual Basic .NET (VB.NET), " +
		                    "is a multi-paradigm, object-oriented programming language, implemented on " +
		                    ".NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the" +
							"successor to its original Visual Basic language"
		Dim wordCount As Dictionary(Of String, Integer) = CountWords(str)

        For Each word In wordCount
            Console.WriteLine($"{word.Key}: {word.Value}")
        Next
    End Sub

    Public Shared Function CountWords(ByVal input As String) As Dictionary(Of String, Integer)
        Dim wordCount = New Dictionary(Of String, Integer)(StringComparer.OrdinalIgnoreCase)
		Dim words = input.Split({" "c, "."c, ","c, "!"c, "?"c, ";"c, ":"c, "-"c, Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

        For Each word In words

            If wordCount.ContainsKey(word) Then
                wordCount(word) += 1
            Else
                wordCount(word) = 1
            End If
        Next

        Return wordCount
    End Function
End Class



' run:
'
' Visual: 3
' Basic: 3
' (VB): 1
' originally: 1
' called: 1
' NET: 4
' (VB: 1
' NET): 1
' is: 1
' a: 1
' multi: 1
' paradigm: 1
' object: 1
' oriented: 1
' programming: 1
' language: 2
' implemented: 1
' on: 1
' Mono: 1
' and: 1
' the: 1
' Framework: 1
' Microsoft: 1
' launched: 1
' VB: 1
' in: 1
' 2002: 1
' as: 1
' thesuccessor: 1
' to: 1
' its: 1
' original: 1
'  

 



answered Mar 1, 2025 by avibootz

Related questions

...