Results 1 to 7 of 7

Thread: Separating string

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    What would be the most efficient way to separate this kind of string: "Word1 Word2 Word3 Word4 Word5", and put each word to separate textbox?

    Thanks in advance.

    ------------------
    Visual Basic Programmer (at least I want to be one)
    ------------------
    PolComSoft
    You will hear a lot about it.


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

    Post

    If you have VB6 you can use the Split function like this:
    Code:
        Dim strResults() As String
        Dim intCount As Integer
        
        Const strText = "Word1 Word2 Word3 Word4 Word5"
        strResults() = Split(strText, " ")
        
        For intCount = 0 To UBound(strResults)
            myTextbox(intCount).Text = strResults(intCount)
        Next intCount
    The above assumes your TextBoxes are a control array. If not then you'll need to remove the loop and move the strResults entries individually.

    ------------------
    Marty

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    Unfortunately I don't have VB6

    ------------------
    Visual Basic Programmer (at least I want to be one)
    ------------------
    PolComSoft
    You will hear a lot about it.


  4. #4
    Member
    Join Date
    Nov 1999
    Posts
    63

    Post

    Here is a word splitting function that will return the words in an array.

    Code:
    Public Function WordSplit(sWords As String) As Variant
    
        Dim sTemp As String
        Dim vWords() As Variant
        Dim n, nCount As Integer
        
        nCount = 0
        sTemp = sWords
        
        Do
            n = InStr(1, sTemp, " ")
            ReDim Preserve vWords(nCount)
            
            If n = 0 Then
                vWords(nCount) = sTemp
            Else
                vWords(nCount) = Left$(sTemp, n)
                sTemp = Mid$(sTemp, n + 1)
            End If
            
            nCount = nCount + 1
        
        Loop Until n = 0
    
        WordSplit = vWords
    
    End Function
    -- Gerald M

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

    Post

    OK, do the following instead:
    Code:
        Dim intStartPos As Integer
        Dim intSpacePos As Integer
        Dim intOldPos As Integer
        Const strText = "Word1 Word2 Word3 Word4 Word5"
        
        intStartPos = 1
        intSpacePos = -1
        
        'Parse the semicolon-delimited input record, looping until no spaces
        'are found
        Do Until intSpacePos = 0
            intSpacePos = InStr(intStartPos, strText, " ", 1)
            If intSpacePos > 0 Then
                MsgBox Mid(strText, intStartPos, intSpacePos - intOldPos)
            Else
                Exit Do
            End If
            intStartPos = intSpacePos + 1
            intOldPos = intStartPos
        Loop
    Replace the MsgBox statement with something that updates your textboxes.

    ------------------
    Marty

  6. #6
    Member
    Join Date
    Nov 1999
    Posts
    63

    Post

    Thank you Marty. I'm aware of the overhead involved with using variant data types, but I completely forgot about the Dim statement in regards to explicitly defining the data type.
    Thanks again for reminding me, Gerald.


    Code:
    Private Sub Command1_Click()
    
        Dim sText As String
        Dim aWords() As String
        Dim n As Integer, nMax As Integer
        
        sText = "Word1 Word2 Word3 Word4 Word5"
        
        WordSplit sText, aWords
    
        nMax = UBound(aWords)
    
        For n = 0 To nMax
            ' Update your text boxes.
        Next
        
    End Sub
    
    Public Sub WordSplit(s As String, aWords() As String)
    
        Dim sTemp As String
        Dim n As Integer, nCount As Integer
    
        nCount = 0
        sTemp = s
    
        Do
            n = InStr(1, sTemp, " ")
            ReDim Preserve aWords(nCount)
            
            If n = 0 Then
                aWords(nCount) = sTemp
            Else
                aWords(nCount) = Left$(sTemp, n)
                sTemp = Mid$(sTemp, n + 1)
            End If
            
            nCount = nCount + 1
        
        Loop Until n = 0
        
    End Sub

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

    Post

    Gerald: Did you know that when you dimension variables as you did with Dim n, nCount As Integer, that n is a then a Variant rather than an Integer? Also, using variants is slow, so you should probably change vWords to sWords and dimension it as a String.

    ------------------
    Marty

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