Results 1 to 12 of 12

Thread: [RESOLVED] How to get the first word of each line in amultiline textbox?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Resolved [RESOLVED] How to get the first word of each line in amultiline textbox?

    Hello everyone
    I need to know how to get first word of each line in a multiline textbox.
    I think the left function doesn't seem to work in a multiline textbox.
    Thank you

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to get the first word of each line in amultiline textbox?

    Just off the top of my head, I'd tend to put all the text in a string, use Split(sYourString, vbCrLf) on the string to make a string array, and then go through the string array with Instr(s(iPtr), " ") and find the space and then use Left$() to take the first word.

    You'll also have to be careful when there's only one word on a line, or maybe even a blank line.

    Good Luck,
    Elroy

    EDIT1: Here, with a multiline textbox (Text1) and a button (Command1), this code in Form1 will do it:

    Code:
    
    Option Explicit
    
    Private Sub Command1_Click()
        Dim s As String
        Dim sa() As String
        Dim i As Long
        Dim j As Long
    
        s = Text1.Text
        sa = Split(s, vbCrLf)
        s = vbNullString
        For i = LBound(sa) To UBound(sa)
            sa(i) = Trim$(sa(i))
            j = InStr(sa(i), " ")
            If j Then sa(i) = Left$(sa(i), j - 1)
            s = s & sa(i) & vbCrLf
        Next i
        '
        MsgBox "Your first words are: " & vbCrLf & vbCrLf & s
    End Sub
    
    
    Last edited by Elroy; Nov 20th, 2017 at 06:45 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: How to get the first word of each line in amultiline textbox?

    Without checking, does that code work for a line that starts with a space, and then a word, or words, follow? Just something to consider if you had not already done so.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to get the first word of each line in amultiline textbox?

    Hi Sam,

    Well, if you study the code, you'll see that I Trim$()'ed each of the array elements. I did sort of think about that.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to get the first word of each line in amultiline textbox?

    Depends on what you mean by "line."

    The runs of characters separated by vbNewLine are paragraphs, which is completely separate from "lines" which depend on with width of the TextBox and whether or not word-wrapping is on (horizontal scrollbar).

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to get the first word of each line in amultiline textbox?

    Hi Dilettante,

    Yeah, I thought about that too, but decided to ignore that issue. Basically, the way I interpreted his questions was to return the first word of each paragraph.

    Take Care,
    Elroy

    EDIT1: And yes, that makes for a more complex problem. But we'll have to wait and see what Mustaphi means by a "line".
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to get the first word of each line in amultiline textbox?

    You code is fabulous Elroy
    It works like a charm.
    I managed to add some criteria for example to extract only the lines that start with numbers I added this:

    Code:
    If IsNumeric(sa(i)) Then s = s & sa(i) & vbCrLf
    and it worked.

    However in some cases I want to obtain the first and second words but I failed to modify the code in order to make it extract the two first words of each line.
    I tried to modify the left function in tens of ways but without success.
    thank you a lot

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to get the first word of each line in amultiline textbox?

    Mustaphi,

    I working on some other things right now. But basically, you need to create some temporary working strings, and search for the second space (and not the first space) in each of the array elements.

    I'm fine if someone else would like to take my code and improve it for Mustaphi.

    Best Regards,
    Elroy

    EDIT1: Alternatively, and possibly better, you could call Instr twice. The first time, you locate the first space. And then, the second time, you use that space's location +1 as a starting place for Instr.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to get the first word of each line in amultiline textbox?

    Elroy
    I thank you very much for your interest
    Belive me I have been trying for nearly two hours but without success.
    I'll be so gratefull to you if you could modify the code.
    thanks a lot

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to get the first word of each line in amultiline textbox?

    I got it
    Code:
    j = InStr(InStr(1, (sa(i)), " ") + 1, (sa(i)), " ")
    thank you

  11. #11
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: [RESOLVED] How to get the first word of each line in amultiline textbox?

    An alternative approach (which makes only a single pass over the Input-String),
    can be implemented this way:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      ParseWords "a b(c)" & vbCrLf & "1,2:3"
    End Sub
    
    Private Sub NewWordCB(Word$, ByVal LineNr&, ByVal WordNr&) 'a Word-CallBack
      Debug.Print LineNr; WordNr, Word 'zerobased LineNrs (and WordNrs per line)
    End Sub
    
    Private Sub ParseWords(S As String)
    Dim B() As Byte, i&, L&, R&, C&
      B = S & IIf(Right$(S, 1) = vbLf, "", vbCrLf)
      For i = 0 To UBound(B) Step 2
        Select Case B(i) + 256& * B(i + 1)
          Case 1 To 9, 11 To 47, 58 To 64, 91 To 93, 123 To 125 'whitespace-chars
            If L Then NewWordCB Mid$(S, 1 + i \ 2 - L, L), R, C: C = C + 1: L = 0
          Case 10:   R = R + 1: C = 0 'a new Row (incr. R + reset the ColCounter)
          Case Else: L = L + 1 'increment WordLen in case of non-whitespace-chars
        End Select
      Next
    End Sub
    The above has a separated Handler-CallbackRoutine, which prints out:
    Code:
     0  0         a
     0  1         b
     0  2         c
     1  0         1
     1  1         2
     1  2         3
    The NewWordCB(...)-Callback should ease any parsing or interpreting of the incoming words significantly.

    HTH

    Olaf

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] How to get the first word of each line in amultiline textbox?

    Mustaphi,

    Nice work in post #10.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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