[RESOLVED] searcing between two word-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] searcing between two word

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Posts
    1,282

    Resolved [RESOLVED] searcing between two word

    I have in a txt file a line similar:
    ...
    AAAAA a12312312 BBBBBB
    ....

    I need to get only this part: a12312312

    in effect i need to get value between to other string part, how to?

    note:

    the postion in string of AAAAA and BBBBBB is variable

  2. #2
    Addicted Member
    Join Date
    Jun 2010
    Location
    The Keystone State
    Posts
    131

    Re: searcing between two word

    Quote Originally Posted by luca90 View Post
    I need to get only this part: a12312312
    the postion in string of AAAAA and BBBBBB is variable
    Is the string you want to get ALWAYS the same string?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Posts
    1,282

    Re: searcing between two word

    Quote Originally Posted by B61Nuke View Post
    Is the string you want to get ALWAYS the same string?
    If your dubt is about that "a12312312", no!
    Is only an example...

    i can have a variable string with a variable lenght from AAAAAAA and BBBBBBBB

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    8,382

    Re: searcing between two word

    You may want to try Split() with space as the seperator which will break the string into an array. From your example element 1 of the array will hold the data you want.

    Or you can use a combination of Instr() and Mid() to get the data.

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,409

    Re: searcing between two word

    Assuming there are blank spaces before and after the string like in your example:
    Code:
    Private Function getString(s As String, a As String, b As String)
    Dim s_start As Long, s_end As Long
        
        s_start = InStr(1, s, a) + Len(a) + 1
        s_end = InStr(1, s, b) - 2
        
        getString = Mid(s, s_start, s_end + 1 - s_start)
    End Function
    Usage:
    Code:
        MsgBox getString("222 AAAAA a12312312 BBBBBB 333", "AAAAA", "BBBBBB")
    Instr() returns the a String position inside another string
    Mid() returns an String inside another, given the start position and its lengh

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Posts
    1,282

    Re: searcing between two word

    Quote Originally Posted by jcis View Post
    Assuming there are blank spaces before and after the string like in your example:
    Code:
    Private Function getString(s As String, a As String, b As String)
    Dim s_start As Long, s_end As Long
        
        s_start = InStr(1, s, a) + Len(a) + 1
        s_end = InStr(1, s, b) - 2
        
        getString = Mid(s, s_start, s_end + 1 - s_start)
    End Function
    Usage:
    Code:
        MsgBox getString("222 AAAAA a12312312 BBBBBB 333", "AAAAA", "BBBBBB")
    Instr() returns the a String position inside another string
    Mid() returns an String inside another, given the start position and its lengh
    IS PERFECT!

    Sorry me, in other case, for my bad english

    ops... note:

    I have 2 friends argentinos, but they live in Italy from 40 years
    Last edited by luca90; Feb 26th, 2012 at 03:24 PM.

  7. #7
    Addicted Member
    Join Date
    Jun 2010
    Location
    The Keystone State
    Posts
    131

    Re: searcing between two word

    Quote Originally Posted by luca90 View Post
    If your dubt is about that "a12312312", no!
    Is only an example...

    i can have a variable string with a variable lenght from AAAAAAA and BBBBBBBB
    I was only asking and used your example you posted. I know your string was not the same as the example. Your answer about the variable string and the length was critical info to get you the correct answer. thats why I had asked.

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.