Jump to content

StringBetween Function


Ic3c0ld
 Share

Recommended Posts

I know this isn't a great forum for this but I am sure someone can help.

Func _StringBetween($s, $from, $to)

$x = StringInStr($s, $from) + StringLen($from)

$y = StringInStr(StringTrimLeft($s, $x), $to)

Return StringMid($s, $x, $y)

EndFunc

I am trying to make this AutoIT script into VB.net Yes I know this isn't a VB.net forum but its coming from autoitscript so I thought it best to ask here.

Link to comment
Share on other sites

I think you might find this page quite helpful.

I have read that already.

Public Function StringBetween(ByVal Source As String, ByVal Start As String, ByVal EndStr As String) As String
        Dim StartPoint As Integer
        Dim EndPoint As Integer
        If InStr(Source, Start) Then
            StartPoint = InStr(Source, Start) + (Len(Start))
            EndPoint = InStr(VB.Left(Source, StartPoint), EndStr)
            Return Mid(Source, StartPoint, EndPoint)
        End If
 End Function

I tried doing this.

But i keep getting this warning

Warning 2 Function 'StringBetween' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Documents and Settings\IceCold\Desktop\Parse\Form1.vb 169 5 OpenUrl

Link to comment
Share on other sites

That's because as it's written, the function won't return anything when the two surrounding strings aren't found. Try this (or a working equivalent -- I don't do VB):

Public Function StringBetween(ByVal Source As String, ByVal Start As String, ByVal EndStr As String) As String
        Dim StartPoint As Integer
        Dim EndPoint As Integer
        If InStr(Source, Start) Then
            StartPoint = InStr(Source, Start) + (Len(Start))
            EndPoint = InStr(VB.Left(Source, StartPoint), EndStr)
            Return Mid(Source, StartPoint, EndPoint)
        End If
        ' The two surrounding strings weren't found
        ' Return an empty string?
        Return ""
End Function
Link to comment
Share on other sites

  • 3 months later...

Hi,

which one is the best or is there even a better one out there? I prefer Nr.1. :)

What about adding the func to the latest beta? Jon??? Others???

Who wants the func to be added?

$string = "Hello UDF _StringBetween()! I like this Autoit-function!"

MsgBox(0, "Func 1 _StringBetween1", _
"1 : " & _StringBetween1($string, "UDF", "!") & @CRLF & _
"2 : " & _StringBetween1($string, "!")& @CRLF & _
"3 : " & _StringBetween1($string, "", "!")& @CRLF & _
"4 : " & _StringBetween1($string, "!"))


Func _StringBetween1($s_String, $s_Start = 0, $s_End = 0)
    $s_Start = StringInStr($s_String, $s_Start) + StringLen($s_Start)
    Return StringMid($s_String, $s_Start, StringInStr($s_String, $s_End) - $s_Start)
EndFunc  ;==>_StringBetween1

MsgBox(0, "Func 2 _StringBetween2", _
"1 : " & _StringBetween2($string, "UDF", "!") & @CRLF & _
"2 : " & _StringBetween2($string, "!")& @CRLF & _
"3 : " & _StringBetween2($string, "", "!")& @CRLF & _
"4 : " & _StringBetween2($string, "!"))

Func _StringBetween2($s, $from = 0, $to = 0)
    $x = StringInStr($s, $from) + StringLen($from)
    $y = StringInStr(StringTrimLeft($s, $x), $to)
    Return StringMid($s, $x, $y)
EndFunc  ;==>_StringBetween2


MsgBox(0, "Func 3 _StringBetween3", _
"1 : " & _StringBetween3($string, "UDF", "!") & @CRLF & _
"2 : " & _StringBetween3($string, "!")& @CRLF & _
"3 : " & _StringBetween3($string, "", "!")& @CRLF & _
"4 : " & _StringBetween3($string, "!"))

Func _stringBetween3($str, $start = 0, $end = 0)
    $pos = StringInStr($str, $start)
    If Not @error Then
        $str = StringTrimLeft($str, $pos + StringLen($start) - 1)
        $pos = StringInStr($str, $end)
        If Not @error Then
            $str = StringTrimRight($str, StringLen($str) - $pos + 1)
            Return $str
        EndIf
    EndIf
EndFunc  ;==>_stringBetween3

Surely, there has to be a description and an error handling...

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...