Results 1 to 6 of 6

Thread: Extract section from string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Extract section from string

    I have the following string in a richtextbox. I need to be able to get the section in () for example in the first one it would be (L0087/06). Whats the best way to do this?

    Code:
    Q)EGTT/QILXX/I/NBO/A/000/999/5131N00235W005
    B)06/01/10 15:00 UTC C)PERM                       AGA  (L0087/06)
    E)RWY 27 ILS LOCALISER RESTRICTION.
      IN THE SECTOR 27-28 DEGREES RIGHT OF CENTERLINE IT SHOULD BE NOTED
      THAT FALSE CAPTURE MAY OCCUR IN THESE AREAS WITH CERTAIN TYPES OF
      AIRCRAFT NAVIGATION RECEIVER.
      UK AIP AD 2-EGTG-1-6 REFERS
    
    Q)EGTT/QIDLT/I/NBO/A/000/999/5131N00235W005
    B)06/01/06 00:01 UTC C)06/12/31 23:59             COM  (L0036/06)
    E)RWY 09 ILS DME 110.55 CH427 MAY UNLOCK INSIDE 4 DME FROM TOUCHDOWN.
      ANY DISCREPANCIES SHOULD BE REPORTED TO ATC 01179364748.
    
    
    Q)EGTT/QSTLT/IV/NBO/A/000/999/5131N00235W005
    B)06/03/18 09:00 UTC C)06/05/07 16:00             RAC  (L0887/06)
    E)FISO ONLY ON WEEKENDS 0900-1700 LOCAL
      ATC SERVICE MAY BE MADE AVBL SUBJECT TO STAFF AND 24HRS NOTICE

  2. #2
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    Re: Extract section from string

    hmm...
    I would use Instr() to find the location of the first open bracket, then the location of the first closed bracket relative to the open bracket, and from there extract all text between those two locations using Mid().

    Do you understand what I mean?
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  3. #3
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Extract section from string

    Here is an example
    VB Code:
    1. Private Function GetSubString(iPos As Integer, strX As String) As String
    2. 'returns the string between "(" and ")"
    3.     Dim aPos As Integer, bPos As Integer
    4.     If iPos >= Len(strX) Then Exit Function
    5.     If iPos <= 0 Then iPos = 1
    6.     aPos = InStr(iPos, strX, "(")
    7.     If aPos = 0 Then Exit Function
    8.     bPos = InStr(aPos, strX, ")")
    9.     If bPos = 0 Then Exit Function
    10.     GetSubString = Mid(strX, aPos + 1, bPos - aPos - 1)
    11.     iPos = bPos + 1
    12. End Function
    13.  
    14. Private Sub Command1_Click()
    15. 'Each time the button is pushed the next substring will be extracted
    16.     Static iPos As Integer
    17.     Dim strX As String
    18.     strX = GetSubString(iPos, RTB.Text)
    19.     If strX = "" Then
    20.         Debug.Print "No more"
    21.     Else
    22.         Debug.Print strX
    23.     End If
    24. End Sub

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Extract section from string

    Split(RTB.Text, ...) on the delimeter combination of vbCrLf & "E)", you will get a string array with the data you need at the end of each string (array member).

    Use Right(... , 10) on each array member to get the data enclosed in parenthesis, lets call this string sRightResult. I assumed all the strings you wanted are 10 chars in length including the parenthesis.

    Use Mid(..., 2, 8) on sRightResult to get a string without the parenthesis.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Extract section from string

    Cheers thanks for all of that.

    Now how would I get the section after the e) for example in the first one it would be "RWY 27 ILS LOCALISER RESTRICTION.
    IN THE SECTOR 27-28 DEGREES RIGHT OF CENTERLINE IT SHOULD BE NOTED
    THAT FALSE CAPTURE MAY OCCUR IN THESE AREAS WITH CERTAIN TYPES OF
    AIRCRAFT NAVIGATION RECEIVER.
    UK AIP AD 2-EGTG-1-6 REFERS"

    cheers

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Extract section from string

    If you used my implementation then it would be at the start of the string (as opposed to the end in your previous requirement) in each array member, starting at array index 1 for zero based arrays.

    You would just have to truncate the excess string starting at the combination vbCrLf & "Q)"

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