quick question

Discussion in 'Code Snippets and Tutorials' started by ceneret0023, Apr 15, 2009.

  1. ceneret0023

    ceneret0023 Level III

    Joined:
    Jun 10, 2007
    Messages:
    663
    Likes Received:
    15
    Location:
    under a rock
    If i'm using the GetStringBetween function how can i make it move to the next line with the start and end val?

    for example:

    <img src="google.com/page1">
    <img src="google.com/page2">

    and i want it to tell me what's in between google.com/ and the last " how would i have it tell me page1 and then loop back through and tell me page2?

    i know the for i= lbound to ubound thing i mean have the function go to the next line of the source.


    thanks in advanced :)
     
  2. Brian

    Brian Level I

    Joined:
    Apr 14, 2009
    Messages:
    92
    Likes Received:
    8
    Location:
    Daytona Beach, FL
    Basically what you are doing is searching a string for a smaller string, it return's the char position of what you are searching for so instead of starting at the beginning you need to make it start from where your search ended. not that difficult..
    So... This need some changes(just to the function declaration) but it should give you a good idea of what is needed...
    Code (Text):
    1.  
    2.     Dim LastResult As String
    3.     Public Function ForEach(ByVal Source As String, ByVal Start As String, ByVal Finish As String) As String
    4.         Dim Result = ""
    5.         Dim LastResult As String = ""
    6.  
    7.         Dim A = InStr(1, Source, Start) + Len(Start)
    8.  
    9.         Do Until Result = ""
    10.             If A = 0 Then
    11.                 Result = ""
    12.                 Return Result
    13.             Else
    14.                 Dim B = InStr(A, Source, Finish)
    15.                 If B = 0 Then
    16.                     Result = ""
    17.                     Return Result
    18.                 Else
    19.                     Result = Mid(Source, A, B - A)
    20.                     Return Result
    21.                     LastResult = Result
    22.                     A = InStr(B, Source, Start) + Len(Start)
    23.                 End If
    24.             End If
    25.         Loop
    26.     End Function
    27.  
     
  3. ceneret0023

    ceneret0023 Level III

    Joined:
    Jun 10, 2007
    Messages:
    663
    Likes Received:
    15
    Location:
    under a rock
    I've tried using your code to adapt mine to yours but whenever i try to use it nothing happens, i hit my command button and the program just sits there, it doesn't even act like it's working. Also what is the code you put up in? i'm using vb6 and a lot of the functions you used don't work with vb6...
     
  4. Brian

    Brian Level I

    Joined:
    Apr 14, 2009
    Messages:
    92
    Likes Received:
    8
    Location:
    Daytona Beach, FL
    Here to make life easy as possible for you..
    Code (Text):
    1.  
    2. Public Function ExtractAll(SearchString As String, StartString As String, EndString As String, AddToList As ListBox) As String
    3.  
    4. Dim Temp As Long, temp1 As Long, temp2 As Long, temp3 As Long
    5. temp1 = 1
    6.  
    7. Do While InStr(temp1, SearchString, StartString, vbTextCompare) <> 0
    8. Temp = InStr(temp1, SearchString, StartString, vbTextCompare) + Len(StartString)
    9. temp2 = InStr(Temp, SearchString, EndString, vbTextCompare)
    10. temp3 = temp2 - Temp
    11. temp1 = temp2 + 1
    12. AddToList.AddItem (Mid$(SearchString, Temp, temp3))
    13. Loop
    14. End Function
    15.  
     
  5. ceneret0023

    ceneret0023 Level III

    Joined:
    Jun 10, 2007
    Messages:
    663
    Likes Received:
    15
    Location:
    under a rock
    i understand what i need to do and what the code is doing(that's the code i've been using), looking through your first code really helped me understand what i needed to do, its just i'm having trouble applying it to the code. What happens when i try to add the loop is either it doesn't do anything at all when i click my command button or it just gives me tons of blank spaces. I wasn't trying to ask for the exact code, i'm sorry if that's what it sounded like, but i'm just having trouble applying what you've said i should do.

    Code (Text):
    1. Public Function GetStringBetween(ByVal InputText As String, _
    2.      ByVal StartText As String, _
    3.      ByVal EndText As String, _
    4.      Optional ByVal StartPosition = 1) As String
    5.  
    6.      Dim lnTextStart As Long
    7.      Dim lnTextEnd As Long
    8.  
    9.     Do Until GetStringBetween = ""
    10.      lnTextStart = InStr(StartPosition, InputText, StartText, vbTextCompare) + Len(StartText)
    11.      lnTextEnd = InStr(lnTextStart, InputText, EndText, vbTextCompare)
    12.      If lnTextStart >= (StartPosition + Len(StartText)) And lnTextEnd > lnTextStart Then
    13.          GetStringBetween = Mid$(InputText, lnTextStart, lnTextEnd - lnTextStart)
    14.          StartPosition = lnTextEnd
    15.      Else
    16.          GetStringBetween = ""
    17.      End If
    18.     Loop
    19.    
    20.  End Function
    this is what i've changed it to and i know i'm probably using what you said too directly but from what i gather it only needs the one loop to have it go back around and search again from the end of the last search the StartPosition = lnTextEnd and it needs to do that until the GetStringBetween="" and i know its proabably not quite as simple as that but that's what i've gathered from looking at your code and trying to use what you told me...
     
  6. Brian

    Brian Level I

    Joined:
    Apr 14, 2009
    Messages:
    92
    Likes Received:
    8
    Location:
    Daytona Beach, FL
    Well this is in .net but it works and you should get the idea here.
    Code (Text):
    1.  
    2.     Public Function GetAll(ByRef Source As String, ByRef Start As String, ByRef Finish As String, ByVal myListbox As ListBox, Optional ByRef iStart As Integer = 0) As String
    3.         Dim iFinish As Integer
    4.         Dim Result As String
    5.         Dim FoundAll As Boolean = False
    6.         iStart = InStr(1, Source, Start) + Len(Start)
    7.         Do While iStart > 0
    8.             Result = String.Empty
    9.             iFinish = InStr(iStart, Source, Finish)
    10.             If iFinish = 0 Then
    11.                 FoundAll = True
    12.             End If
    13.             Result = Mid(Source, iStart, iFinish - iStart)
    14.             iStart = InStr(iFinish, Source, Start) + Len(Start)
    15.             SetText(ListBox_Items, Result)
    16.             If InStr(iStart, Source, Start, CompareMethod.Binary) = 0 Then
    17.                 Exit Do
    18.             End If
    19.         Loop
    20.     End Function
    21.  
     
  7. Snowman

    Snowman Level IV

    Joined:
    Apr 9, 2007
    Messages:
    825
    Likes Received:
    0
    add theses two functions

    Public Function ParseRight(What As String, StopAt As String) As String
    On Error GoTo PErr
    ParseRight = Mid(What, InStr(What, StopAt) - 1)

    Exit Function
    PErr:
    ParseRight = "Search string does not exist"
    End Function

    Public Function ParseLeft(What As String, StopAt As String) As String
    On Error GoTo PErr
    ParseLeft = Left(What, InStr(What, StopAt) - 1)

    Exit Function
    PErr:
    ParseLeft = "Search string does not exist"
    End Function

    so do this:

    newtext.text = Parseright(text.text, "src=")
    newtext.text = parseleft(newtext.text, "img src")
    whatyouwant.text = getbetween(newtext.text, ".com/", ">")
    whatyouwant.text = Replace(whatyouwant.text, ",", "")

    thatll get you page1

    newtext2.text = Parseright(text2.text, ".com")
    whatyouwant.text = getbetween(newtext.text, ".com/", ">")
    whatyouwant.text = Replace(whatyouwant.text, ",", "")

    that should get you page 2

    tell me how it goes
     
  8. ceneret0023

    ceneret0023 Level III

    Joined:
    Jun 10, 2007
    Messages:
    663
    Likes Received:
    15
    Location:
    under a rock
    Thanks for the help Snowmann but i had already figured it out... i really appreciate your trying to help though :)
     
  9. Snowman

    Snowman Level IV

    Joined:
    Apr 9, 2007
    Messages:
    825
    Likes Received:
    0
    ohhh sorry hahaha. but keep these parse function, theyre really good when trying to single out text.