matrixninja2
asked on
count text Characters in vb.net
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.ToStri ng()
txtWords.Text = pintText.Words.ToString()
txtSentences.Text = pintText.Sentences.ToStrin g()
txtParagraphs.Text = pintText.Paragraphs.ToStri ng()
End Sub
End Class
Thanks in advance for your help.
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
Dim pintText As New StringProcessor
pintText.StringContents = txtFile.Text
txtChars.Text = pintText.Characters.ToStri
txtWords.Text = pintText.Words.ToString()
txtSentences.Text = pintText.Sentences.ToStrin
txtParagraphs.Text = pintText.Paragraphs.ToStri
End Sub
End Class
Thanks in advance for your help.
ASKER
Thats the problem I am lost on how to add that data.
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
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
ASKER
I am pretty new So I would not know how to even write that code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Thanks guys that did help me understand.
Dave