Results 1 to 10 of 10

Thread: How to read a string until a certain character(s) are reached?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    How to read a string until a certain character(s) are reached?

    Hello how are you?
    how can you read that line, and return everything until "a" or "ar" is reached, resulting in "Hello how "?

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,428
    By using Left$ or Mid$.

    VB Code:
    1. Dim sBuff As String: sBuff = "Hello how are you?"
    2.  
    3.     MsgBox Left$(sBuff, InStr(sBuff, " a") - 1)

    Note: The ' ' (space) preceeding the a!
    Also, there are limitations to this approach.

    Experiment with Mid$ too.





    Bruce.

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,428
    Left$ Returns a String containing a specified number of characters from the left side of a string.

    Syntax

    Left$(string, length)

  4. #4
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    234

    Re: How to read a string until a certain character(s) are reached?

    Edit : Wrong thread answered.

    Delete this

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to read a string until a certain character(s) are reached?

    Quote Originally Posted by Bruce Fox View Post
    VB Code:
    1. Dim sBuff As String: sBuff = "Hello how are you?"
    2.  
    3.     MsgBox Left$(sBuff, InStr(sBuff, " a") - 1)
    May not be what the OP wants, but the problem should be described better.

    Does the 'a' or 'ar' need to be the start of a word, anywhere in the word, or at the end of the word: army, caring, peculiar

    If looking for just 'a', what is wanted if the word was just: a
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: How to read a string until a certain character(s) are reached?

    These are my lovely routines...
    You can make an xml or a html parser using these..
    A$="Hello how are you?"
    B$="ar"
    ' a$ is passing by reference so we get a changed a$
    C$=GetStrUntil(b$,a$,false)
    ' You can split without removing "ar"
    ? c$, a$

    If we want to drop until "ar"

    A$="Hello how are you?"
    B$="ar
    DropLeft b$, A$
    ' but now we have this "e you?"

    You can use "<" for GetStrUntil a tag start with removing
    You can use ">" for GetStrUntil a tag start with removing
    A$=html$
    a1$=GetStrUntil("<",a$)
    tagS1$=GetStrUntil(">",a$)
    data1$=GetStrUntil("<",a$)
    tagE1$=GetStrUntil(">",a$)

    ' dropleft used to get something after a specific string or nothing
    We can move a line next or a$ will be empty..
    dropleft vbcrlf , a$



    Code:
    Public Sub DropLeft(ByVal uStr As String, fromStr As String)
    Dim i As Long
    i = InStr(fromStr, uStr)
    If i > 0 Then
    fromStr = Mid$(fromStr, i + Len(uStr))
    Else
    fromStr = ""
    End If
    End Sub
    
    Public Function GetStrUntil(ByVal sStr As String, fromStr As String, Optional RemoveSstr As Boolean = True) As String
    Dim i As Long
    If fromStr = "" Then GetStrUntil = "": Exit Function
    i = InStr(fromStr, sStr)
    If i < 2 Then
    GetStrUntil = ""
    fromStr = ""
    Else
    GetStrUntil = Left$(fromStr, i - 1)
    If RemoveSstr Then
    fromStr = Mid$(fromStr, Len(sStr) + i)
    Else
    fromStr = Mid$(fromStr, i)
    End If
    End If
    End Function

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to read a string until a certain character(s) are reached?

    ummm you guys do know that this thread is over 10 years old, right?

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to read a string until a certain character(s) are reached?

    Quote Originally Posted by DataMiser View Post
    ummm you guys do know that this thread is over 10 years old, right?
    Color me embarrassed
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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

    Re: How to read a string until a certain character(s) are reached?

    From his posting history it looks like a .Nutter had gotten lost in here, started to pontificate, then burnt his baby and bailed.

  10. #10
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: How to read a string until a certain character(s) are reached?


    @DataMiser
    you are right..........

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