vb.net - get string between

Hello,
I am using this code to get string between two inputs but I would like to modify it
for example:

right now if you have something like this

Dim name as string = "my name is Paul Roman"
TextBox1.text = GetStringBetween(name, "name is " , "Roman")

that works perfectly fine and it extracts the name correctly

but for example lets say that I have about 30 lines

my name is Paul1 Roman1
my name is Paul2 Roman2
my name is Paul3 Roman3
my name is Paul4 Roman4
etc...

I would like to modify the code so that it finds in this example: "Paul3 Roman3"
and it extract the name "Paul3"

how can I do this to go in reverse like: input1 " Roman3" and imput2 "name is "


    Function GetStringBetween(ByVal Str1 As String, ByVal str2 As String, ByVal str3 As String, Optional doReverse As Boolean = False) As String
            Dim foundstr As String = ""
            Dim i As Integer = Str1.ToUpper().IndexOf(str2.ToUpper())
            Dim j As Integer
            If i > -1 Then
                Str1 = Str1.Substring(i + str2.Length)
                j = Str1.ToUpper().IndexOf(str3.ToUpper())
                If j > -1 Then
                    foundstr = Str1.Substring(0, j)
                End If
            End If
            Return foundstr
    End Function

Open in new window

LVL 1
XK8ERAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What goal are you trying to achieve by going in reverse?
0
XK8ERAuthor Commented:
im going to use this function to parse data from html files and also from text files because sometimes going in reverse is the only way to extract some strings..
0
XK8ERAuthor Commented:
okay, I think that after 4 hrs of trying to get this crapola running i've found a nice little h4ck that works by reserving the strings



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim test = "my name is Paul1 Roman1 my name is Paul2 Roman2 my name is Paul3 Roman3 my name is Paul4 Roman4"
        test = GetStringBetween(test, " Roman3 ", "name is ", True)
        Debug.Print(test)
    End Sub

    Function GetStringBetween(ByVal Str1 As String, ByVal str2 As String, ByVal str3 As String, Optional iReverse As Boolean = False) As String
            If iReverse = True Then
                Str1 = ReverseString(Str1)
                str2 = ReverseString(str2)
                str3 = ReverseString(str3)
            End If

            Dim foundstr As String = ""
            Dim i As Integer = Str1.ToUpper().IndexOf(str2.ToUpper())
            Dim j As Integer
            If i > -1 Then
                Str1 = Str1.Substring(i + str2.Length)
                j = Str1.ToUpper().IndexOf(str3.ToUpper())
                If j > -1 Then
                    foundstr = Str1.Substring(0, j)
                End If
            End If
            If iReverse = True Then
                Return ReverseString(foundstr)
            Else
                Return foundstr
            End If
    End Function

    Function ReverseString(ByRef strString As String) As String
            Dim NextChr, TempString As String, StringLength, Count As Integer, NewString As String = Nothing

            TempString = strString
            StringLength = Len(TempString)
            Do While Count <= StringLength
                Count = Count + 1
                NextChr = Mid(TempString, Count, 1)
                NewString = NextChr & NewString
            Loop
            ReverseString = NewString
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

käµfm³d 👽Commented:
You should simply need to incorporate a strategic use of the LastIndexOf method:

    Function GetStringBetween(ByVal Str1 As String, ByVal str2 As String, ByVal str3 As String, Optional doReverse As Boolean = False) As String
        Dim foundstr As String = ""
        Dim i As Integer
        Dim j As Integer

        If doReverse Then
            i = Str1.LastIndexOf(str2, StringComparison.OrdinalIgnoreCase)
        Else
            i = Str1.IndexOf(str2, StringComparison.OrdinalIgnoreCase)
        End If

        If i > -1 Then
            Str1 = Str1.Substring(i + str2.Length)

            If doReverse Then
                j = Str1.LastIndexOf(str3, StringComparison.OrdinalIgnoreCase)
            Else
                j = Str1.IndexOf(str3, StringComparison.OrdinalIgnoreCase)
            End If

            If j > -1 Then
                foundstr = Str1.Substring(0, j)
            End If
        End If

        Return foundstr
    End Function

Open in new window

0
XK8ERAuthor Commented:
kaufmed, thanks did you see my previous post?
0
käµfm³d 👽Commented:
Yeah, but I don't think you need the extra overhead of creating a duplicate, reversed string. Give me a few minutes to get to work, and I'll post a shortened version of your original method.
0
käµfm³d 👽Commented:
When you originally stated that you'd be looking in reverse, did you mean you would looking at the words of the string in reverse order, or are you actually wanting to look at the characters (of the string) in reverse order?
0
XK8ERAuthor Commented:
the output of your code is "my" which is incorrect.. it should be "Paul3" like the code I posted above!

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim test = "my name is Paul1 Roman1 my name is Paul2 Roman2 my name is Paul3 Roman3 my name is Paul4 Roman4"
        test = GetStringBetween(test, " Roman3 ", "name is ", True)
        Debug.Print(test)
    End Sub

    Function GetStringBetween(ByVal Str1 As String, ByVal str2 As String, ByVal str3 As String, Optional doReverse As Boolean = False) As String
        Dim foundstr As String = ""
        Dim i As Integer
        Dim j As Integer

        If doReverse Then
            i = Str1.LastIndexOf(str2, StringComparison.OrdinalIgnoreCase)
        Else
            i = Str1.IndexOf(str2, StringComparison.OrdinalIgnoreCase)
        End If

        If i > -1 Then
            Str1 = Str1.Substring(i + str2.Length)

            If doReverse Then
                j = Str1.LastIndexOf(str3, StringComparison.OrdinalIgnoreCase)
            Else
                j = Str1.IndexOf(str3, StringComparison.OrdinalIgnoreCase)
            End If

            If j > -1 Then
                foundstr = Str1.Substring(0, j)
            End If
        End If

        Return foundstr
    End Function
End Class

Open in new window

0
käµfm³d 👽Commented:
What does the parameter iReverse represent:  That the parameters have been sent into the function in reverse order, or that you will be searching the string backwards?
0
XK8ERAuthor Commented:
like this

test = GetStringBetween(test, "name is", "Roman3", True)

or like this

test = GetStringBetween(test, "Roman3", "name is", True)

your code's output is "my"

you should test the code I posted and the output is "Paul3"

can you see the difference?
0
käµfm³d 👽Commented:
I understand that the code is incorrect, and I concur. In order to post a proper fix, I am trying to ascertain what the intent of that variable is.
0
XK8ERAuthor Commented:
I found the answer myself
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.