Raised This Month: $ Target: $400
 0% 

[USEFUL FUNCTION] StrToken


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode