Results 1 to 2 of 2

Thread: Count words inside a string

  1. #1

    Thread Starter
    Member imj75's Avatar
    Join Date
    Aug 2000
    Location
    South Africa,Pretoria
    Posts
    51

    Post

    How do I count the amount of words in a string? I'm using a string to store integers (maximum of 6) for reference. The numbers in the string will not exceed 5 digits each, and are seperated by a space, although this can be changed to something else. I've searched a lot of sites but found nothing of use.

  2. #2
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    If you have VB6 then you can do this:

    Code:
    Dim IntArr() As String
    
    IntArr = Split(TheIntStringYouAreUsing, " ")
    
    AmoutOfWordsInString = UBound(IntArr) + 1
    But I think you would be much better off using a dynamic array to store these variables:

    Code:
    '(Any VB)
    'Declaration section:
    Dim IntArr() As Integer
    'Somewhere in code:
    'ElNum is the number of elements you want the array to have (in this case 5)
    ElNum = 5
    ReDim Preserve IntArr(ElNum) As Integer
    'IntArr is now array which has 6 elements (0, 1, 2, 3, 4, 5)
    So you can do this:
    IntArr(0) = 1932
    IntArr(1) = 3629
    IntArr(2) = 2374
    IntArr(3) = 8235
    IntArr(4) = 6983
    IntArr(5) = 217
    If you want to add more elements just do this:
    Code:
    ReDim Preserve IntArr(7) As Integer
    'You have already set 0,1,2,3,4, and 5
    IntArr(6) = 6983
    IntArr(7) = 217
    To read the values, just do this
    Code:
    Debug.Print "Fifth element = " & IntArr(4)
    Code:
    'And to find the amount of elements in the array:
    Debug.Print UBound(IntArr) & "Elements in array"
    'Out: 7 Elements in array
    'You must always add one to find the amount of elements in the array:
    Debug.Print UBound(IntArr) + 1 & "Elements in array"
    'Out: 8 Elements in array. That's better.
    I hope this helps.

    BTW: Since you are a new member, you don't really know how this forum works. A question like this should go in 'General Questions'. This forum is really only for complaints (hundred of complaints ) about the forum.
    Courgettes.

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