I have a public function that i use quite a lot in the program that im making, that takes a string in-between 2 values:
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
So basically if i do this:
Dim Testpage as string = "hello my name is jayfella and i am seeking help" Dim MyOutput as string MyOutput = getstringbetween(testpage, "name", "seeking")
MyOutput will return: "is jayfella and i am"
Which is exactly what i want to do, but this is a very lame and slow way to go about it because it uses very old methods. The time it takes using this method is such that it interferes the timing of my program; the GetStringBetween function does not finish fast enough, so i have to insert a thread.sleep (sometimes for over 1500ms(!)) just for the program to wait for the GetStringBetween function! and that is my problem
I understand vb.net has vastly improved its number-crunching and searching abilities, but im struggling to figure out how to do it with as little coding as possible.
I have tried this:
Public Function midReturn(ByVal total As String, ByVal first As String, ByVal last As String) If last.Length < 1 Then midReturn = total.Substring(total.IndexOf(first)) End If If first.Length < 1 Then midReturn = total.Substring(0, (total.IndexOf(last))) End If Try midReturn = ((total.Substring(total.IndexOf(first), (total.IndexOf(last) - total.IndexOf(first)))).Replace(first, "")).Replace(last, "") Catch ArgumentOutOfRangeException As Exception midReturn = "ERROR" End Try
Which pretty much does exactly the same thing, except it doesnt seem to work for me because the text i search through is HTML, so can contain pretty much any character in the universe, and that function doesnt cope very well with that (i think its because of the use of "indexof") :S
Does anybody have a simple and efficient way of doing this, without the "special character" complications?