Results 1 to 9 of 9

Thread: [RESOLVED] count the number of words

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Resolved [RESOLVED] count the number of words

    i have a richtextbox
    if i clicked any word i need to get the number of the same words in the text
    which are befor the word clicked

    example text:
    University of Liverpool

    Founded in 1881, the University of Liverpool has earned an international reputation for high quality and is one of the most highly respected educational institutions in the UK. It has been associated with no fewer than eight Nobel Laureates.

    Laureate Online Education, formerly known as KIT eLearning, has acted as the e-learning partner of the University of Liverpool since 1999. It specialises in delivering 100% online academic education on a global basis - tailored for the needs of working professionals worldwide.

    Laureate Online Education has developed Europe's truly international online Master programmes, using specially adapted versions of University of Liverpool campus-taught degrees.




    if i clicked the word University that is underlined
    i should get the value =3
    by ignoring the last word because it cames after the one choosen


    how can i do this

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: count the number of words

    VB Code:
    1. Dim strWords() As String
    2. Dim intCount As Integer
    3. Dim lngIndex As Long
    4.  
    5.  
    6. strWords = Split(MyData, " ")
    7.  
    8. For lngIndex = 0 To UBound(strWords)
    9.     If Left$(LCase(strWords(lngIndex)), Len("university")) = "university" Then
    10.         intCount = intCount + 1
    11.     End If
    12. Next
    13.  
    14. MsgBox intCount - 1
    The Left$ portion of the If statement takes care of the situation where the word you want to count is followed by a period, comma, etc.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: count the number of words

    i need the code to be general for any word
    University is just an example

    and what is MyData ?

  4. #4
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: count the number of words

    Why not count the spaces.
    Code:
    For a=1 to len(LineOfText)
        if Mid(LineOfText(a,1)=" " then Count=count+1
    next a
    ' and for the last word of the sentence
    count=count+1
    This is very simple way of counting

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: count the number of words

    Quote Originally Posted by om-yousif
    i need the code to be general for any word
    University is just an example

    and what is MyData ?
    MyData was just a placeholder for my testing, and I assumed you knew how to tell which word was selected. Here is a better example.

    VB Code:
    1. Dim strWords() As String
    2. Dim intCount As Integer
    3. Dim lngIndex As Long
    4. Dim strSelected As String
    5.  
    6. strSelected = LCase(Trim$(RichTextBox1.SelText))
    7. strWords = Split(RichTextBox1.Text, " ")
    8.  
    9. For lngIndex = 0 To UBound(strWords)
    10.     If Left$(LCase(strWords(lngIndex)), Len(strSelected)) = strSelected Then
    11.         intCount = intCount + 1
    12.     End If
    13. Next
    14.  
    15. MsgBox intCount - 1

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: count the number of words

    MartinLiss
    this code will count all words in the text and i need it to stop counting when it reaches the word selected


    i.e # words befor selected

  7. #7
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: count the number of words

    Yes, I saw that after I posted. I must remember to read the thread i.e. all the words not just the ones I like.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: count the number of words

    thanks ZenDisaster
    this is good but i need to get the count# without changing colors

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: count the number of words

    Yes thats it

    can it ignor any word between brackets
    i mean if the same word was between ( ) it wil not be counted
    there may be more than one word between ( )

    example:
    University of Liverpool

    Founded in 1881, the (University of Liverpool has earned )an international reputation for high quality and is one of the most highly respected educational institutions in the UK. It has been associated with no fewer than eight Nobel Laureates.

    Laureate Online Education, formerly known as KIT eLearning, has acted as the e-learning partner of the University of Liverpool since 1999. It specialises in delivering 100% online academic education on a global basis - tailored for the needs of working professionals worldwide.


    if i choose the underlined word it should return 2 so it will ignor the word which is between ( )
    Last edited by om-yousif; Dec 26th, 2006 at 01:39 PM.

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