Results 1 to 6 of 6

Thread: Character Counting In a Textfile

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    13

    Character Counting In a Textfile

    How do i count the number of visible characters in a text file in vb 2008 (ignoring spaces, tabs, carriage returns, spaces etc) ?
    I need to display this amount in a label.
    Any help is appreciated
    thanks

  2. #2
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Character Counting In a Textfile

    Suggestion: Load it in a textbox and do the count via .Lenght. Or am I thinking to simple?


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    13

    Re: Character Counting In a Textfile

    I'm not sure could you provide sample code?
    for returning only visible characters. Because when i do it it returns carriage returns, spaces and tabs with the character count

  4. #4
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Character Counting In a Textfile

    Try this. Somebody else might have a better solution though.

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim VisibleCharacters As Integer = 0
    3.         Dim SingleCharacter As Char() = File.ReadAllText("C:\Test.txt").ToCharArray
    4.  
    5.         For i As Integer = 0 To SingleCharacter.Length - 1
    6.             Select Case SingleCharacter(i)
    7.                 Case ControlChars.Tab
    8.                 Case ControlChars.Lf
    9.                 Case ControlChars.Cr
    10.                 Case " "
    11.                 Case Else
    12.                     VisibleCharacters += 1
    13.             End Select
    14.         Next i
    15.  
    16.         Label1.Text = String.Format("Total: {0} characters ({1} characters are visible).", SingleCharacter.Length, VisibleCharacters)
    17.  
    18.     End Sub

    If you don't want to use an array, like you said in the PM you've sent me, then you can use Substring.

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim VisibleCharacters As Integer = 0
    3.  
    4.         Dim AllText As String = File.ReadAllText("C:\Text.txt")
    5.         For i As Integer = 0 To AllText.Length - 1
    6.             Select Case AllText.Substring(i, 1)
    7.                 Case ControlChars.Tab
    8.                 Case ControlChars.Lf
    9.                 Case ControlChars.Cr
    10.                 Case " "
    11.                 Case Else
    12.                     VisibleCharacters += 1
    13.             End Select
    14.         Next
    15.  
    16.         Label1.Text = String.Format("Total: {0} characters ({1} characters are visible).", AllText.Length, VisibleCharacters)
    17.  
    18.     End Sub
    Last edited by Chris001; Nov 18th, 2011 at 03:37 PM. Reason: typo

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Character Counting In a Textfile

    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

  6. #6
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Character Counting In a Textfile

    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim inputString = String.Format("Peter Piper {0} picked a peck of {1} pickled peppers", ControlChars.NewLine, ControlChars.Tab)
            Dim visibleCount = CountVisibleCharacters(inputString)
        End Sub
    
        Private Function CountVisibleCharacters(s As String) As Integer
            Return System.Text.RegularExpressions.Regex.Matches(s, "[\x21-\x7E]").Count
        End Function
    http://www.regular-expressions.info/posixbrackets.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width