[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

count text Characters in vb.net

Posted on 2004-11-03
7
Medium Priority
?
516 Views
Last Modified: 2010-04-17
I have a question how would set vb.net to count text that is in a text box. I would like to call it from a class. This is what I have in my class but it returns zeros.

Imports System
Imports System.IO
Public Class StringProcessor
    Private numCharacters As Integer
    Private numWords As Integer
    Private numSentences As Integer
    Private numParagraphs As Integer
    Private StringData As String
    Public ReadOnly Property Characters() As Integer
        Get
            Return numCharacters
        End Get
    End Property
    Public ReadOnly Property Words() As Integer
        Get
            Return numWords
        End Get
    End Property
    Public ReadOnly Property Sentences() As Integer
        Get
            Return numSentences
        End Get
    End Property
    Public ReadOnly Property Paragraphs() As Integer
        Get
            Return numParagraphs
        End Get
    End Property

    Public Property StringContents() As String
        Set(ByVal Value As String)
            numCharacters = 0
            numWords = 0
            numSentences = 0
            numParagraphs = 0
        End Set
        Get
            Return StringData
        End Get
    End Property
End Class

This is what I have in my main form:

Imports Course
Imports System
Imports System.IO


    Private Sub btnAnalyzeText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyzeText.Click
        Dim pintText As New StringProcessor

        pintText.StringContents = txtFile.Text
        txtChars.Text = pintText.Characters.ToString()
        txtWords.Text = pintText.Words.ToString()
        txtSentences.Text = pintText.Sentences.ToString()
        txtParagraphs.Text = pintText.Paragraphs.ToString()
       
    End Sub
End Class


Thanks in advance for your help.
0
Comment
Question by:matrixninja2
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Expert Comment

by:dcgames
ID: 12487124
This is obviously incomplete. In your StringContents() implementation you don't store the string data, nor do you count the chars, words, sentences or paragraphs.

Dave
0
 

Author Comment

by:matrixninja2
ID: 12487200
Thats the problem I am lost on how to add that data.
0
 
LVL 3

Expert Comment

by:vippx
ID: 12491540
Hi,

you could write the logic for spearation in the set part of your string contents property.

The Number of characters is simple, just the Length of the string would give u that...

As for word count, u can use the String.Split() method to split the strings by the use of a delimiting character or multiple characters(plenty of examples in MSDN), which would be a space(" ") for standard text. If you need advanced processing, run a search for "string tokenizers for .NET" and you would have a few examples.

Sentence count also can be found by splitting the string using a delimiter as a "."

Paragraph count is could get tricky, but principle remains same, here try a split with vbNewLine constant which represents the newline character.

if u need more help let me know

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:matrixninja2
ID: 12496251
I am pretty new So I would not know how to even write that code.
0
 
LVL 5

Accepted Solution

by:
dcgames earned 1000 total points
ID: 12500608
I'm sorry but this sounds like homework to me and we're not supposed to give you the answer in detail.

What I can say is that you have a string passed in as a parameter and that string has a length, so it is fairly trivial to have a loop over the entire length of the string and look at each character.

Brute force will do the job, you don't have to be very sophisticated.

to get you started

            stringData = Value
            numCharacters = stringData.Length()
            numWords = 0
            numSentences = 0
            numParagraphs = 0
            doingWord=false

Now do a loop and look at every character. If it is a letter or digit, set a flag to indicate you are looking at a word. If it's anything else and you were looking at a word, then count a word and reset the flag.

If you see a period, you could assume sentence, I guess.

If you see a carriage return (or maybe a line feed), you could assume a paragraph.

And that's it.

0
 
LVL 3

Expert Comment

by:vippx
ID: 12502563
Hi as dcgames rightly said, we can help you but coding it for u is not the purpose.

The string.split() is an easy way to tokenize strings. however if you wish to implment the approach from scratch like dcgames suggested, you should look up more information on text parsers. An important concept that would help you is the use of a lookahead character

That is when you loop through the string, u read a character and also the immediate next one, called the  look ahead character. and the combination of the read character and the lookahead, put it in a select case statement and see if u found characters like space, comma, semicolon, period etc which would denote end of a word.  

example most often current character being period( . ) and look ahead character being a space(" ") means you have reached the end of a sentence

if you hit a character as space(" ") and lookahead is also a spacwe(" ") you know there is an extra space and the next whitespace should be ignored.

Using similiar logic you can fine tune your parse the string the way you like.

hope this helps
0
 

Author Comment

by:matrixninja2
ID: 12511068
Thanks guys that did help me understand.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question