Jump to content
Sign in to follow this  
Ic3c0ld

StringBetween Function

Recommended Posts

Ic3c0ld

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.

Share this post


Link to post
Share on other sites
Ic3c0ld

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

Share this post


Link to post
Share on other sites
LxP

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

Share this post


Link to post
Share on other sites
Xenobiologist

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

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.