[RESOLVED] String Seperation of words-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] String Seperation of words

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2016
    Posts
    22

    Resolved [RESOLVED] String Seperation of words

    I have string in which there are 3 to 4 words
    i wanted to separate all 4 word in individual 4 string variables

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,203

    Re: String Seperation of words

    You can use the Split() function to create an array from them
    Code:
    Option Explicit
    
    Private Sub Command_Click()
    Dim S() As String, X As Integer
    S = Split("this is some string", " ")
    For X = 0 To UBound(S)
        Debug.Print S(X)
    Next
    End Sub
    Output is:

    this
    is
    some
    string

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,065

    Re: String Seperation of words

    How about something like:

    Code:
    Option Explicit
    
    Private Declare Function StrSpn Lib "shlwapi" Alias "StrSpnW" ( _
        ByVal pszStr As Long, _
        ByVal pszSet As Long) As Long
    
    Private Declare Function StrCSpn Lib "shlwapi" Alias "StrCSpnW" ( _
        ByVal pszStr As Long, _
        ByVal pszSet As Long) As Long
    
    Private Function ScanUntil( _
        ByVal Start As Long, _
        ByRef Text As String, _
        ByRef CharSet As String) As Long
        'Returns 1-based character offset of first character in CharSet.
    
        If Start > Len(Text) Then
            ScanUntil = Len(Text) + 1
            Exit Function
        End If
    
        ScanUntil = Start _
                  + StrCSpn(&H80000000 Xor ((StrPtr(Text) Xor &H80000000) + (Start - 1) * 2), _
                            StrPtr(CharSet))
    End Function
    
    Private Function ScanWhile( _
        ByVal Start As Long, _
        ByRef Text As String, _
        ByRef CharSet As String) As Long
        'Returns 1-based character offset past all characters in CharSet.
    
        If Start > Len(Text) Then
            ScanWhile = Len(Text) + 1
            Exit Function
        End If
    
        ScanWhile = Start _
                  + StrSpn(&H80000000 Xor ((StrPtr(Text) Xor &H80000000) + (Start - 1) * 2), _
                           StrPtr(CharSet))
    End Function
    
    Private Sub Command1_Click()
        Const PUNCTUATION As String = " -,;.?!"
        Dim Text As String
        Dim WordStart As Long
        Dim PastWord As Long
    
        Text = Text1.Text
        WordStart = 1
        List1.Clear
        Do Until WordStart > Len(Text)
            WordStart = ScanWhile(WordStart, Text, PUNCTUATION)
            PastWord = ScanUntil(WordStart, Text, PUNCTUATION)
            List1.AddItem Mid$(Text, WordStart, PastWord - WordStart)
            WordStart = PastWord
        Loop
    End Sub
    Name:  sshot.png
Views: 43
Size:  2.7 KB
    Attached Files Attached Files
    Last edited by dilettante; Jan 11th, 2018 at 12:37 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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.