Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to count character, words, sentences and paragraphs

How to count character, words, sentences and paragraphs

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    justmeTW
    wrote on last edited by
    #1

    I have a really big problem here. This is what I need to do: Create a Class Library (I did that)in this class it will open a *.* file and once that file is open it needs to read that file and then count the number of characters, words, sentences and Paragraphs. I created a ReadOnly Property for each: Ex: Public ReadOnly Property as Characters as Integer For each of these properties they need to have hidden variables for the ReadOnly property to return. The hidden variables is where I need the calucaltion to be at to count the characters, words,...ect. Can someone just give me one code for either one of the above counts and I should beable to go from there? Thanks in advance!

    J 1 Reply Last reply
    0
    • J justmeTW

      I have a really big problem here. This is what I need to do: Create a Class Library (I did that)in this class it will open a *.* file and once that file is open it needs to read that file and then count the number of characters, words, sentences and Paragraphs. I created a ReadOnly Property for each: Ex: Public ReadOnly Property as Characters as Integer For each of these properties they need to have hidden variables for the ReadOnly property to return. The hidden variables is where I need the calucaltion to be at to count the characters, words,...ect. Can someone just give me one code for either one of the above counts and I should beable to go from there? Thanks in advance!

      J Offline
      J Offline
      John Kuhn
      wrote on last edited by
      #2

      Sorry -- I am a bit confused; do you need functions that determine the counts, or do you need to know how to code the accessors for the properties? What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

      J 1 Reply Last reply
      0
      • J John Kuhn

        Sorry -- I am a bit confused; do you need functions that determine the counts, or do you need to know how to code the accessors for the properties? What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

        J Offline
        J Offline
        justmeTW
        wrote on last edited by
        #3

        Actually an example of both would be nice. Thanks

        J 1 Reply Last reply
        0
        • J justmeTW

          Actually an example of both would be nice. Thanks

          J Offline
          J Offline
          John Kuhn
          wrote on last edited by
          #4

          OK, this is a pretty basic example -- the way you parse words and sentences can be way more complex -- but it is a starting point...

          Imports System.IO
          Imports System.Text

          Public Class FileCounts

          Private \_text As String
          Private \_name As String
          Private \_sentences As Integer
          Private \_words As Integer
          Private \_characters As Integer
          
          Public ReadOnly Property Sentences()
              Get
                  Return \_sentences
              End Get
          End Property
          
          Public ReadOnly Property Words()
              Get
                  Return \_words
              End Get
          End Property
          
          Public ReadOnly Property Characters()
              Get
                  Return \_characters
              End Get
          End Property
          
          Public Sub New(ByVal filename As String)
              If File.Exists(filename) Then
                  \_name = filename
                  \_readFile()
                  \_characters = \_text.Length
                  \_sentences = \_text.Split(".").Length
                  \_words = \_text.Split(" ").Length
              Else
                  Throw New ApplicationException("File " + filename + " does not exist")
              End If
          End Sub
          
          Private Sub \_readFile()
              Dim sb As StringBuilder = New StringBuilder
              Try
                  Dim fs As FileStream = File.Open(\_name, FileMode.Open, FileAccess.Read, FileShare.None)
                  Dim b(1024) As Byte
                  Dim temp As UTF8Encoding = New UTF8Encoding(True)
                  While fs.Read(b, 0, b.Length) > 0
                      sb.Append(temp.GetString(b))
                  End While
              Catch ex As Exception
                  Console.WriteLine(ex.Message)
              End Try
              \_text = sb.ToString()
          End Sub
          

          End Class

          What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups