Results 1 to 19 of 19

Thread: Count words in a text box

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60

    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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Private Function GetWordCount(ByVal ctrl As Control) As Short
    2.         Dim words() As String = ctrl.Text.Split(" ")
    3.         Return words.Length
    4.     End Function
    5.  
    6. 'syntax of use
    7. 'MsgBox(GetWordCount(RichTextBox1))

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    Thank you. How would I display this value in a label in real-time mode then? Thanks,

    Andrew

  4. #4
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    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")

  6. #6
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    You asked these same questions about 2 weeks ago..
    http://www.dotnetforums.net/showthre...hlight=regular

    Scroll down to the bottom for my reply.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    Thanks for the reply. I am confused, but will try it and let you know how it works. Andrew

  8. #8
    Member
    Join Date
    Nov 2003
    Posts
    33

    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.

  9. #9
    Junior Member
    Join Date
    Nov 2003
    Posts
    25
    VB Code:
    1. ' returns word count
    2. ' correction for double/triple/ multiple spaces
    3. ' input is the string
    4. Public Function wordCount(rawS As String) As Long
    5. Dim spcSpltsA() As String
    6. Dim wrdCntI As Long
    7. Dim i As Integer
    8. spcSpltsA = Split(rawS, " ")
    9. wrdCntI = UBound(spcSpltsA)
    10.  
    11. ' if empty string in array than reduce count - indicates extra space
    12. For i = 1 To UBound(spcSpltsA)
    13.     If spcSpltsA(i) = "" Then
    14.         wrdCntI = wrdCntI - 1
    15.     End If
    16. Next
    17. wordCount = wrdCntI
    18. End Function
    - tushar

  10. #10
    Member
    Join Date
    Nov 2003
    Posts
    33

    project

    any ideas on a sentence count or paragraph count?

  11. #11
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  12. #12
    Member
    Join Date
    Nov 2003
    Posts
    33

    ..

    sorry im just new to programming and I find it confusing, this is my first program

  13. #13
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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))

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Dim sentencePattern As String = "\b(\.$|\. |\.\n|\.\r|\?$|\? |\?\n|\?\r|!$|! |!\n|!\r)"
    2.         Dim wordPattern As String = ("\w+")
    3.         MsgBox("Sentence Count: " & Regex.Matches(TextBox1.Text, sentencePattern).Count)
    4.         MsgBox("Word Count: " & Regex.Matches(TextBox1.Text, wordpattern).Count)

  15. #15
    Junior Member
    Join Date
    Nov 2003
    Posts
    25
    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:
    1. <script type="text/vbscript">
    2.  
    3.  
    4. Function RegExpCnt(str1)
    5.  
    6.     Dim regEx, Match, Matches       ' Create variable.
    7.     Set regEx = New RegExp
    8.  
    9.  
    10.     Dim sentencePattern
    11.     sentencePattern = "\b(\.$|\. |\.\n|\.\r|\?$|\? |\?\n|\?\r|!$|! |!\n|!\r)"
    12.     Dim wordPattern
    13.     wordPattern = "\w+"
    14.     MsgBox(str1)
    15.     MsgBox("Sentence Count: " & Regex.Matches(str1, sentencePattern).Count)
    16.     MsgBox("Word Count: " & Regex.Matches(str1, wordpattern).Count)
    17. End Function
    18.  
    19.  
    20. RegExpCnt ("hello   how are u ? today ! m  k")
    21.  
    22. </script>

    but it does not work
    - tushar

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  17. #17
    Member
    Join Date
    Nov 2003
    Posts
    33

    Angry

    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.

  18. #18
    Member
    Join Date
    Nov 2003
    Posts
    33

    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

  19. #19
    Member
    Join Date
    Nov 2003
    Posts
    33

    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
  •  



Click Here to Expand Forum to Full Width