how would I count the number of paragraphs and sentences of a text file in a text box?
Printable View
how would I count the number of paragraphs and sentences of a text file in a text box?
What do you mean by a sentence?! If you mean anything that is ended by a '.' , '!' or '?' and combinations like "?!!!" is a sentence then you might apply a method to count them and thats easy. And for paragraphs, again you have to define it. if you define a paragraph where "Enter' is pressed and you go to another line you may get the line numbers of the text file, that is ReadLine of streamreader till you are at the end of file, or count the number of Linefeed/Carriage Return characters.
I want to count sentences ending with a period, question mark, or exclamation mark. And for paragraphs I guess I would count every enter or double enter as a paragraph. Im not really sure how to count the paragraphs. I dont know what the code should look like.
You may use RegEx to count them.
I was going to use the regex method. But my reacher told me it was to high a level of a function. I almost have my paragraph count working the only fault in it is that if i have 3 paragraphs in the textbox it only counts 2 should I automatically add 1 to the paragraph variable at the end or should I add some other line of code here is what I have so far. I am counting 2 carriage returns as a paragraph.
Dim sr As IO.StreamReader = IO.File.OpenText(filename)
Dim theline As String
TextBox2.Clear()
TextBox2.Multiline = True
Do While sr.Peek <> -1
theline = sr.ReadLine
If theline.Length = 0 Then
paragraph = paragraph + 1
End If
TextBox2.Text = TextBox2.Text + theline + vbCrLf
Loop
sr.Close()
TextBox1.Text = filename
End Sub
I forgot to tell you that I am counting the paragraphs as they are put into the textbox with the streamreader.