none
Get string between indexes? Please.

    Question

  • I've been searching for hours trying to make a Button event that removes all the text between two indexes in a list box. The two indexes are #FIRSTNAMES# and #LASTNAMES#... I have a code but it's WAAAY too confusing and it does not work. If anyone knows a way to do this, please let me know.
    I'll post my code just in case you don't know another way. I haven't gotten to the remove part, I'm still just trying to display the text between the indexes into a message box. I took most of it from Google pages... I really want an easier way, because I know there is one, but never the less I just need progress.

    WARNING: THE BELOW CODE CONTAINS EXTREMELY BAD CODING AND WILL GIVE YOU BAD DREAMS. DO NOT EVEN LOOK AT IT UNLESS YOU DON'T KNOW ANOTHER WAY.








    '#############BUTTON EVENT###################
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton5.Click
            abovesection = "#FIRSTNAMES#"
            belowsection = "#LASTNAMES#"
            dpremove()
        End Sub
    
    '###############SHOW STRING BETWEEN#########3333
        Private Sub dpremove()
            Dim p As String = secobox.Items.IndexOf(belowsection)
            Dim a As String = secobox.Items.IndexOf(abovesection)
            Dim MyOutput As String
       For Each item In ListBox1.Items
                MyOutput = GetStringBetween(item, a, p)
                MsgBox(MyOutput)
            Next
        End Sub
    '######GET STRING BETWEEN#######
        Public Function GetStringBetween(ByVal InputText As String, _
            ByVal starttext As String, _
            ByVal endtext As String)
    
            Dim lnTextStart As Long
            Dim lnTextEnd As Long
    
            lnTextStart = InStr(StartPosition, InputText, starttext, vbTextCompare) + Len(starttext)
            lnTextEnd = InStr(lnTextStart, InputText, endtext, vbTextCompare)
            If lnTextStart >= (StartPosition + Len(starttext)) And lnTextEnd > lnTextStart Then
                GetStringBetween = Mid$(InputText, lnTextStart, lnTextEnd - lnTextStart)
            Else
                GetStringBetween = "ERROR"
            End If
        End Function
    


    • Edited by Humpasaur Wednesday, January 13, 2010 12:17 PM Grammar again..
    Wednesday, January 13, 2010 12:15 PM

Answers

  • hi,

    You can try with regular expression like this :

    Dim regex As System.Text.RegularExpressions.Regex = _
    New System.Text.RegularExpressions.Regex("(?<=#FIRSTNAMES#).*(?=#LASTNAMES#)")
    
    Dim match As System.Text.RegularExpressions.Match = _
    regex.Match("#FIRSTNAMES#I'm glad to help u#LASTNAMES#")
    
    If (match.Success = true) Then
        Console.WriteLine(match.Value)
    Else
        Console.WriteLine("not found")
    End If


    Hope this helps,

    Mathieu
    Mathieu Francesch Sharplog Engineering
    • Marked as answer by Humpasaur Friday, January 15, 2010 7:26 AM
    Wednesday, January 13, 2010 1:16 PM

All replies

  • stop your complaint and explain this:
    why are you speaking about a listbox, and then about a string part?
    You've a listbox with a number of elements, one is (for example) 'firstname' and another is (for example) 'lastname'
    if you want to remove the elements between these, you have one issue.

    but if you've an element with 'firstname' and 'lastname' in it and you want remove the part of the string between the two words, you have another issue.

    I suggest you to write Option Strict On as first line of your code.
    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Wednesday, January 13, 2010 1:11 PM
  • hi,

    You can try with regular expression like this :

    Dim regex As System.Text.RegularExpressions.Regex = _
    New System.Text.RegularExpressions.Regex("(?<=#FIRSTNAMES#).*(?=#LASTNAMES#)")
    
    Dim match As System.Text.RegularExpressions.Match = _
    regex.Match("#FIRSTNAMES#I'm glad to help u#LASTNAMES#")
    
    If (match.Success = true) Then
        Console.WriteLine(match.Value)
    Else
        Console.WriteLine("not found")
    End If


    Hope this helps,

    Mathieu
    Mathieu Francesch Sharplog Engineering
    • Marked as answer by Humpasaur Friday, January 15, 2010 7:26 AM
    Wednesday, January 13, 2010 1:16 PM