If you are okay with a little LINQ the following reports back the character count minus characters in the array UnWantedValues then reports back the total including UnWantedValues array characters. Lastly a language extension method provides a line count of the file.
To use simple change the first line to a file you want to check on.
Code:Module Module2 Public Sub Demo() Dim Lines = IO.File.ReadAllText(IO.Path.Combine(Application.StartupPath, "Document.txt")) Dim CharGroup = _ ( _ From c In Lines.ToCharArray() _ Group c By c Into Group _ Select New With {.Item = c, .Occurrences = Group.Count, .Numb = Asc(c)}).ToList _ .OrderBy(Function(x) x.Item) Dim UnWantedValues As Integer() = {9, 10, 13, 32} Dim Total = (From Item In CharGroup _ Where Not UnWantedValues.Contains(Item.Numb) _ Select Item.Occurrences).Sum Dim Total1 = (From Item In CharGroup Select Item.Occurrences).Sum Console.WriteLine("Total less unwanted {0} Total {1}", Total, Total1) Console.WriteLine("Line count: {0}", Lines.LineCount) End Sub <System.Diagnostics.DebuggerStepThrough()> _ <Runtime.CompilerServices.Extension()> _ Public Function LineCount(ByVal value As String) As Integer Return System.Text.RegularExpressions.Regex.Matches(value, ".+\n*").Count End Function End Module




Reply With Quote