.NET GetTok() - mIRC Function

By Jonesy44 on Dec 17, 2009

A recent PHP Script was posted which contained "ports" of the token functions from mIRC which can be very helpful

So, i gave it a go in VB.NET Starting off with GetTok(), i've attempted to include all of the sub functions too, including negative tokens, and ranges.

I'll start working on the others such as AddTok() etc.

Not sure if this is the efficient method. Any other ideas, give me a shout :)

    Public Function GetTok(ByVal Str As String, ByVal Token As String, ByVal Delimiter As Short)
        ' Syntax:  GetTok(String, ([Negative]Token)|(Token[-RangeToken]), Delimiter)
        ' Example: GetTok("This is my string", "3", 32)
        '   returns - "my"
        ' Example: GetTok("This is my string", "-1", 32)
        '   returns - "string"
        ' Example: GetTok("This is my string", "2-3", 32)
        ' -------------------------------------------
        If Str Is String.Empty Then
            Return "Error, Empty string"
        ElseIf Delimiter < 1 Or Delimiter > 255 Then
            Return "Error, Delimiter must be between 1-255"
        Else
            Dim CheckRgx As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^(\d+)-(\d*)$")
            If CheckRgx.IsMatch(Token) Then
                Dim SplitArray() As String = Split(Str, Chr(Delimiter)), Returnex As String = ""
                Dim Range() As String = CheckRgx.Split(Token)
                For Index As Integer = Range(1) To Range(2)
                    Returnex += IIf(Returnex IsNot String.Empty, Chr(Delimiter), "") & SplitArray(Index - 1)
                Next
                Return Returnex
            Else
                If Token = 0 Then
                    Return "Error, token cannot be = 0"
                ElseIf Token > 0 Then
                    Dim SplitArray() As String = Split(Str, Chr(Delimiter))
                    Return SplitArray(Token - 1)
                ElseIf Token < 0 Then
                    Dim SplitArray() As String = Split(Str, Chr(Delimiter))
                    Return SplitArray(SplitArray.Length + Token)
                End If
            End If
        End If
    End Function

Comments

Sign in to comment.
Jonesy44   -  Dec 18, 2009

Yeah, i suppose so.

 Respond  
RJosh   -  Dec 17, 2009

hehe yea me too. But $gettok is inherited from $token if i'm not mistaken, so all functions of $token were transfered over including the 0 function. It made more sense using it with $token then it does with $gettok though.

 Respond  
Jonesy44   -  Dec 17, 2009

This is very true! I'll edit the post. i've always used $numtok() for that function.

 Respond  
RJosh   -  Dec 17, 2009

Why can't token by a 0? A 0 should return the total amount of tokens if you're planning on replicating the actual $gettok method from mIRC.

Other then that good job. I've been working on something similar in C#, never got around to finishing.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.