|
-
Dec 2nd, 2002, 08:52 PM
#1
Thread Starter
Member
Count words in a text box
I have to count specific letters entered in a text box (which I've completeed, as well as count the total number of words. I am going to call anything with a space before and after it a word. I'm not really sure what to do for this. If someone has an idea it would be greatly appreciated! Thank you.
Here is my code to count a character...
Code:
' Count the number of times that character a is used in the string InputText
Function strCountA1(ByVal InputText As String, ByVal a As Char) As Integer
Dim i As Integer
For i = 0 To InputText.Length - 1
If InputText.Substring(i, 1) = a Then
strCountA1 = strCountA1 + 1
End If
Next
End Function
-
Dec 2nd, 2002, 09:04 PM
#2
VB Code:
Private Function GetWordCount(ByVal ctrl As Control) As Short
Dim words() As String = ctrl.Text.Split(" ")
Return words.Length
End Function
'syntax of use
'MsgBox(GetWordCount(RichTextBox1))
-
Dec 2nd, 2002, 11:14 PM
#3
Thread Starter
Member
Thank you. How would I display this value in a label in real-time mode then? Thanks,
Andrew
-
Dec 3rd, 2002, 02:00 AM
#4
Member
I replied to this on the other forum you posted to as well, but just incase here it is on this forum as well...
Regular expressions are the easiest and best way if you want an accurate count of words in a String..
Code:
Dim r As New System.Text.RegularExpressions.Regex("\w+")
WordCountLabel.Text = r.Matches(InputText).Count
You can easily count how many times a is in a String as well...
Code:
Dim r As New System.Text.RegularExpressions.Regex("[a]{1}")
strCountA1 = r.Matches(InputText).Count
Last edited by wyrd; Dec 3rd, 2002 at 02:08 AM.
-
Dec 3rd, 2002, 11:26 PM
#5
Thread Starter
Member
Thanks, that worked good. How should I count the total characters in the text box? I can't seem to get anything to work very well. The code that I posted above doesn't work at all. I need to count the total of letters typed in the alphabet (A-Z). Any ideas?
ALSO - I need to count the instances of "st" in the text box. The code that I'm using now does work, but if "s" is not the last character in the text box, then it displays "0" instead of the total. How can I fix this? Thanks much.
My "st" counting code:
Code:
' Count the number of times that characters st are used in the string InputText
Function CountSTTotal(ByVal InputText As String, ByVal t As Char) As Integer
Dim i As Integer
If InputText.EndsWith("s") Or InputText.EndsWith("S") Then
For i = 0 To InputText.Length - 1
If InputText.Substring(i, 1) = t Then
CountSTTotal = CountSTTotal + 1
End If
Next
End If
End Function
Me.STCountLabel.Text = CountSTTotal(InputTextBox.Text, "t")
-
Dec 4th, 2002, 01:24 AM
#6
Member
You asked these same questions about 2 weeks ago..
http://www.dotnetforums.net/showthre...hlight=regular
Scroll down to the bottom for my reply.
-
Dec 4th, 2002, 12:24 PM
#7
Thread Starter
Member
Thanks for the reply. I am confused, but will try it and let you know how it works. Andrew
-
Dec 4th, 2003, 08:01 PM
#8
Member
help
I am writing a program as well that counts the words in a text box the way I would like to do it is to count the spaces. What would the code look like if I wanted to count them I used some code using a split function and It counted every space so even if there was a double space it would count two words when and make the end number false. I dont know what to write for the code.
-
Dec 6th, 2003, 05:43 AM
#9
Junior Member
VB Code:
' returns word count
' correction for double/triple/ multiple spaces
' input is the string
Public Function wordCount(rawS As String) As Long
Dim spcSpltsA() As String
Dim wrdCntI As Long
Dim i As Integer
spcSpltsA = Split(rawS, " ")
wrdCntI = UBound(spcSpltsA)
' if empty string in array than reduce count - indicates extra space
For i = 1 To UBound(spcSpltsA)
If spcSpltsA(i) = "" Then
wrdCntI = wrdCntI - 1
End If
Next
wordCount = wrdCntI
End Function
-
Dec 7th, 2003, 06:20 PM
#10
Member
project
any ideas on a sentence count or paragraph count?
-
Dec 7th, 2003, 09:27 PM
#11
Frenzied Member
I don't want this to sound rude, but think about it. What's the definition of a sentence? What's the definition of a paragraph?
You already have examples of code that works. You should be able to adapt that.
-
Dec 8th, 2003, 01:25 PM
#12
Member
..
sorry im just new to programming and I find it confusing, this is my first program
-
Dec 8th, 2003, 04:13 PM
#13
Frenzied Member
Fair enough. So you need to count sentences, I guess a sentence would end with a period or a question mark. I like Edneeis' idea of using the split function. You might run into trouble if there was an ellipses (...) in the text, but you usually get an approximate count anyway.
You could probably use the same split idea for carriage return/line feed (vbCrLf or hex 0d0a)
There, too you might run into troubles - maybe they hit enter twice between paragraphs, maybe just once. But I bet you could code to check for both.
Alternatively, you could just loop through all of the text and incriment a variable when you hit a period, another variable when you hit a carriage return (hex 0d or Chr(13))
-
Dec 8th, 2003, 06:25 PM
#14
On second thought I agree with wyrd and would say go for regular expressions. I don't know how to get a paragraph though, but here is a sentence and word. The following code assumes an Imports System.Text.RegularExpressions
VB Code:
Dim sentencePattern As String = "\b(\.$|\. |\.\n|\.\r|\?$|\? |\?\n|\?\r|!$|! |!\n|!\r)"
Dim wordPattern As String = ("\w+")
MsgBox("Sentence Count: " & Regex.Matches(TextBox1.Text, sentencePattern).Count)
MsgBox("Word Count: " & Regex.Matches(TextBox1.Text, wordpattern).Count)
-
Dec 9th, 2003, 02:49 AM
#15
Junior Member
regex is nice - if i can get it to work - basic help needed/ what object do i refer to to get the regex library
i tried the code in VBscript theu a html page:
VB Code:
<script type="text/vbscript">
Function RegExpCnt(str1)
Dim regEx, Match, Matches ' Create variable.
Set regEx = New RegExp
Dim sentencePattern
sentencePattern = "\b(\.$|\. |\.\n|\.\r|\?$|\? |\?\n|\?\r|!$|! |!\n|!\r)"
Dim wordPattern
wordPattern = "\w+"
MsgBox(str1)
MsgBox("Sentence Count: " & Regex.Matches(str1, sentencePattern).Count)
MsgBox("Word Count: " & Regex.Matches(str1, wordpattern).Count)
End Function
RegExpCnt ("hello how are u ? today ! m k")
</script>
but it does not work
-
Dec 9th, 2003, 03:44 AM
#16
You would use the imports I mentioned. Although it wont match that string because there are spaces before the punctuation which isn't really vaild for a sentence. It checks the end of words for the punctuation then a space or new line.
-
Dec 9th, 2003, 08:39 PM
#17
Member
i still cant get the regex way of counting senteces to work. I have been trying to do it with a for loop but i cant get it to work this is what i have.
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim sentence As String
Dim i As String
i = TextBox2.Text
For i = 0 To TextBox2.Text.Length
If TextBox2.Text.Substring(i, 1) = "." Then
sentence = sentence + 1
If i = "?" Then sentence = sentence + 1
If i = "!" Then sentence = sentence + 1
End If
Next
TextBox4.Text = sentence
End Sub
I want to click a button and have the number of sentences in a textbox.
-
Dec 9th, 2003, 09:51 PM
#18
Member
change
I got the sentence count to work but and almost have my paragraph count working the only fault in it is that if i have 3 paragraphs 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.
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
Any one have any suggestions
-
Dec 11th, 2003, 03:43 PM
#19
Member
p count
I added 1 to the paragraph counter before I displayed the count of the paragraphs and it seemed to work until I put a cariage return or two at the end of the text file that I open in the text file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|