+92 332 4229 857 99ProjectIdeas@Gmail.com

Count Words In File (Vb.net)



The following code shows you how to count words in file. The function CountWords() accept one parameter as file path from which we have to count words. i.e., CountWords(filePath As String). Strategy is that: we read the file line by line and spliting the line at every space now after spliting the array has all the words excluding spaces now taking length of that array and add it.





Imports System.IO

Public Sub CountWords(ByVal filePath As String)

        Dim tr As TextReader = File.OpenText(filePath)

        Dim words As Integer = 0

        Dim line = tr.ReadLine()

        While Not line Is Nothing

            Dim str = line.Split(" ")

            words = words + str.Length

            line = tr.ReadLine()

        End While

        Console.WriteLine(words.ToString())

End Sub

0 comments: