View Single Post
Author Message
API
Veteran Member
Join Date: May 2006
Old 05-10-2007 , 19:15   [USEFUL FUNCTION] StrToken
Reply With Quote #1

This is an easy way to token for a group secluded by whitespaces.
Example:
StrToken("Hello, this is a test.",3,out,24);
out will end up being "is".

Code:
public OnPluginStart()
{
    new String:test[]="Hello, this is a test string.";
    new String:out[]="";
    StrToken(test,5,out,192);
    PrintToServer("%s",out);
}

stock StrToken(const String:inputstr[],tokennum,String:outputstr[],maxlen)
{
    new String:buf[maxlen+1];
    new cur_idx;
    new idx;
    new curind;
    idx=StrBreak(inputstr,buf,maxlen);
    if(tokennum==1)
    {
        strcopy(outputstr,maxlen,buf);
        return;
    }
    curind=1;
    while (idx!=-1)
    {
        cur_idx+=idx;
        idx=StrBreak(inputstr[cur_idx],buf,maxlen);
        curind++;
        if(tokennum==curind)
        {
            strcopy(outputstr,maxlen,buf);
            break;
        }
    }
}

Last edited by API; 06-26-2007 at 01:48. Reason: more optimized and better method for strsize
API is offline
Send a message via AIM to API