Results 1 to 5 of 5

Thread: parsing text strings

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    Hello,
    I have a text box and I want to be able to pick out just the words individualy.
    I can get the number of spaces and the number of words but I am not quite sure how to get the actuall words.
    if my string is this
    "Hello, how are you doing?"
    I want to be able to associate "hello" with the number 1
    "how" with number two...ect.
    I hope this makes sense to someone.
    thanks
    pnj

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    What version of VB do you have???

    If you have VB 6 then use Split Function.
    example:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim strTest As String: strTest = "Hello, how are you doing?"
        Dim strArray() As String
        Dim I As Integer
        
        strArray() = Split(strTest, " ", , vbTextCompare)
        
        For I = 0 To UBound(strArray)
            If Right(strArray(I), 1) = "," Then '//check for unwanted symbols like ,.:;)( etc
                strArray(I) = Left(strArray(I), Len(strArray(I)) - 1)
            End If
        Next I
        
        For I = 0 To UBound(strArray)
            Debug.Print I + 1 & " - " & strArray(I)
        Next I
        
    End Sub
    
    'It would print:
    '1 - Hello
    '2 - how
    '3 - are
    '4 - you
    '5 - doing?
    Hope this makes some sense

    Post #505

    [Edited by QWERTY on 08-07-2000 at 12:14 PM]

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    If you are using VB6 you can use the Split Function

    Code:
    Dim MyArray() As String
    MyArray() = Split(Text1.Text," ")
    Now MyArray(0) is the first word, MyArray(1) is the Second, etc

  4. #4
    Guest
    If you do not have VB6, you can write your own Split function.

    Code:
    Public Function Split(ByVal sIn As String, Optional sDelim As _
          String, Optional nLimit As Long = -1, Optional bCompare As _
           VbCompareMethod = vbBinaryCompare) As Variant
        Dim sRead As String, sOut() As String, nC As Integer
        If sDelim = "" Then
            Split = sIn
        End If
        sRead = ReadUntil(sIn, sDelim, bCompare)
        Do
            ReDim Preserve sOut(nC)
            sOut(nC) = sRead
            nC = nC + 1
            If nLimit <> -1 And nC >= nLimit Then Exit Do
            sRead = ReadUntil(sIn, sDelim)
        Loop While sRead <> ""
        ReDim Preserve sOut(nC)
        sOut(nC) = sIn
        Split = sOut
    End Function
    
    Public Function ReadUntil(ByRef sIn As String, _
          sDelim As String, Optional bCompare As VbCompareMethod _
        = vbBinaryCompare) As String
        Dim nPos As String
        nPos = InStr(1, sIn, sDelim, bCompare)
        If nPos > 0 Then
            ReadUntil = Left(sIn, nPos - 1)
            sIn = Mid(sIn, nPos + Len(sDelim))
        End If
    End Function

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537

    thanks all

    Wow, I think that was more info than I need right now.
    thanks!
    pnj

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