Tips and tricks for .NET using ASP and VB code.

Get the number of lines, words and characters in a text file

Dim FileContents As String = My.Computer.FileSystem.ReadAllText("C:\Temp\Temp.TXT")
Dim LinesFound As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(FileContents, "\n")
Dim WordsFound As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(FileContents, "\w+")
Dim NumberOfLines As ULong = LinesFound.Count
Dim NumberOfWords As ULong = WordsFound.Count
Dim NumberOfCharacters As ULong = FileContents.Length

No comments:

Post a Comment

Search This Blog