Results 1 to 8 of 8

Thread: Learning : How to Grab string from left or maybe right

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Learning : How to Grab string from left or maybe right

    from post 1
    #1 from http://www.vbforums.com/showthread.p...t-data-frp-eft
    am trying to learn more about grabbing all the strings from the left or right.

    please bare in mind these strings are from txt file.
    example of the strings below from txt file that contains large text and we want to only extract these and add them to list1 can you share different methods to extract thanks.

    first extraction test.
    Code:
    xxx.bin not found
    sound.wav not found
    music.bin not found
    texture.bin not found


    second extraction test
    gamelist.zip attached
    Code:
    +---------------+-------+-------------------------------------------------------+---------------+-------+---------------+---------------+---------------------------------------+
    | 1941u		|  	| 1941 - Counter Attack (900227 USA)			| 1941		| 1990	| Capcom	| CPS1		| 					|
    | 1941		|  	| 1941 - Counter Attack (900227 World)			| 		| 1990	| Capcom	| CPS1		| 					|
    | 1941j		|  	| 1941 - Counter Attack (Japan)				| 1941		| 1990	| Capcom	| CPS1		| 					|
    | 1941r1	|  	| 1941 - Counter Attack (World)				| 1941		| 1990	| Capcom	| CPS1		| 					|
    | area88r	|  	| Area 88 (Japan Resale ver.)				| unsquad	| 1989	| Daipro / Capco| CPS1		| 					|
    | area88	|  	| Area 88 (Japan)					| unsquad	| 1989	| Daipro / Capco| CPS1		| 					|
    | punisherbz	|  	| Biaofeng Zhanjing (Chinese bootleg)			| punisher	| 2002	| bootleg	| CPS1		| 					|
    | dino		|  	| Cadillacs & Dinosaurs (930201 etc)			| 		| 1993	| Capcom	| CPS1 / QSound	| 					|
    | dinou		|  	| Cadillacs & Dinosaurs (930201 USA)			| dino		| 1993	| Capcom	| CPS1 / QSound	| 					|
    | dinopic	|  	| Cadillacs and Dinosaurs (bootleg set 1 (with PIC16c57)| dino		| 1993	| Capcom	| CPS1		| No sound				|
    | dinopic2	|  	| Cadillacs and Dinosaurs (bootleg set 2 (with PIC16c57)| dino		| 1993	| Capcom	| CPS1		| No sound				|
    | dinoh		|  	| Cadillacs and Dinosaurs (bootleg set 3, 930223 Asia TW| dino		| 1993	| bootleg	| CPS1 / QSound	| 					|
    | dinoeh	|  	| Cadillacs and Dinosaurs (hack, 930201 etc)		| dino		| 1993	| Capcom	| CPS1 / QSound	| 					|
    | dinot		|  	| Cadillacs and Dinosaurs Turbo (bootleg set 1, 930223 A| dino		| 1993	| bootleg	| CPS1 / QSound	| 					|
    | dinotpic	|  	| Cadillacs and Dinosaurs Turbo (bootleg set 2 (with PIC| dino		| 1993	| bootleg	| CPS1		| No sound				|
    | dinoj		|  	| Cadillacs Kyouryuu-Shinseiki (Cadillacs 930201 Japan)	| dino		| 1993	| Capcom	| CPS1 / QSound	| 					|
    | cworld2j	|  	| Capcom World 2 (920611 Japan)				| 		| 1992	| Capcom	| CPS1		| 					|
    | captcommjr1	|  	| Captain Commando (910928 Japan)			| captcomm	| 1991	| Capcom	| CPS1		| 					|
    want to extract all 1941u and 1941 and 1941j all list. going down below these
    Attached Files Attached Files
    Last edited by doberman2002; Oct 7th, 2019 at 04:08 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Learning : How to Grab string from left or maybe right

    you can use instr to test if one of those values exist in each line, you may need to add leading and/or trailing spaces to only find whole words, eg. so 1941 does not find 1941r, if instr > 0 you can add that line or part of to the listbox
    Code:
    if instr(ln, " 1941u ") > 0 or instr(ln, " 1941 ") > 0 or instr(ln, " 1941j ") > 0 then list1.additem ln
    where ln is a single line from text file

    depending on the size of the text file, you can choose to read the whole text file then split into lines, or use line input to just read the file line by line
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Learning : How to Grab string from left or maybe right

    Try this function
    Code:
    Private Function Find(ByVal strSearchIn As String, ByVal strSearchFor As String) As String()
        Dim sI() As String
        Dim sF() As String
        Dim sRet() As String
        Dim j As Long
        Dim k As Long
        Dim c As Long
        
        sI = Split(strSearchIn, vbNewLine)
        sF = Split(strSearchFor, ",")
        
        For j = 0 To UBound(sI)
            For k = 0 To UBound(sF)
                If InStr(1, sI(j), sF(k), vbTextCompare) > 0 Then
                     ReDim Preserve sRet(c)
                     sRet(c) = sI(j)
                     c = c + 1
                     Exit For
                End If
            Next
        Next
        
        Find = sRet
        
    End Function
    Call it with your file contents and list of strings to search for, it will return list of lines contains any string you are searching for
    sample usage
    Code:
        Dim r() As String
        r = Find(Text1.Text, "1941u,1941j")' Text1 is your file contents 
        Text2.Text = Join(r, vbNewLine)' Text2 shows search result



  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: Learning : How to Grab string from left or maybe right

    4x2y
    westconn1

    Last edited by doberman2002; Oct 7th, 2019 at 04:12 PM.

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Learning : How to Grab string from left or maybe right

    Quote Originally Posted by doberman2002 View Post
    4x2y
    westconn1

    Can you be more clear? First you said that you want to extract 1941u, 1941j, 1941, and now say you want to extract all list!



  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Learning : How to Grab string from left or maybe right

    Try this new version
    Code:
    Private Function Find(ByVal strSearchIn As String) As String()
        Dim sI() As String
        Dim sF() As String
        Dim sRet() As String
        Dim j As Long
        Dim c As Long
        
        sI = Split(strSearchIn, vbNewLine)
        
        For j = 0 To UBound(sI)
            If InStr(1, sI(j), "|") > 0 Then
                sF = Split(sI(j), "|")
                ReDim Preserve sRet(c)
                sRet(c) = Replace$(sF(1), vbTab, vbNullString)
                c = c + 1
            End If
        Next
        
        Find = sRet
        
    End Function



  7. #7
    Member
    Join Date
    Oct 2019
    Posts
    37

    Re: Learning : How to Grab string from left or maybe right

    Hi,
    In this case, just use the text file than a CVS format, using a "|" character to parse the text !
    No?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: Learning : How to Grab string from left or maybe right

    Quote Originally Posted by 4x2y View Post
    Try this new version
    Code:
    Private Function Find(ByVal strSearchIn As String) As String()
        Dim sI() As String
        Dim sF() As String
        Dim sRet() As String
        Dim j As Long
        Dim c As Long
        
        sI = Split(strSearchIn, vbNewLine)
        
        For j = 0 To UBound(sI)
            If InStr(1, sI(j), "|") > 0 Then
                sF = Split(sI(j), "|")
                ReDim Preserve sRet(c)
                sRet(c) = Replace$(sF(1), vbTab, vbNullString)
                c = c + 1
            End If
        Next
        
        Find = sRet
        
    End Function
    Very Good A++++++ Code.
    i have another code like that Post1 but the line is on the right and its a difficult one i will try share it here once i find it.
    https://raw.githubusercontent.com/al...b/gamelist.txt

    ok so changing to 6 did it wow
    sRet(c) = Replace$(sF(6), vbTab, vbNullString)
    Last edited by doberman2002; Oct 7th, 2019 at 07:51 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
  •  



Click Here to Expand Forum to Full Width