Raised This Month: $32 Target: $400
 8% 

[USEFUL FUNCTION] Explode


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Olly
Senior Member
Join Date: Mar 2007
Location: UK
Old 05-11-2007 , 08:46   [USEFUL FUNCTION] Explode
Reply With Quote #1

This function will split a string up by a character you specify, and then store each 'token' in an array.

Example code
Code:
new String:myArray[5][32];

new size = explode(5, "This\nis\nlots\nof\nlines", 31, '\n');

// size will be: 5
// myArray will be {"This", "is", "lots", "of", "lines"}
Code:
/*****************************************************************
 * explode
 *
 * @breif this function will explode a string into an array. Splitting the string by delimiter
 * @params output the 2dimensional array to save to
 * @params p_iMax max entries
 * @params p_szInput input string
 * @params p_iSize leingth of strings
 * @params p_szDelimiter the deliminator to split by
 * @return the size of the array created
 *****************************************************************/
stock explode( String:p_szOutput[][], p_iMax, String:p_szInput[], p_iSize, p_szDelimiter )
{
    new iIdx = 0;
    // get the leingth of string
    new l = strlen(p_szInput);
    // copy the string to ouput upto the delimiter
    new iLen = copyc( p_szOutput[iIdx], p_iSize, p_szInput, p_szDelimiter) + 1;
    // loop through the second dimension
    while( (iLen < l) && (++iIdx < p_iMax+1) )
    {    
        //copy any folowing text to the array, incremenitn the count each time
        iLen += (1 + copyc( p_szOutput[iIdx], p_iSize, p_szInput[iLen], p_szDelimiter));
    }
    return iIdx+1;
}

/*****************************************************************
 * copyc
 *
 * @breif this function will copy a string upto a defined character
 * @params dest the place to save to
 * @params len leingth of the string
 * @params src input string
 * @params ch character to stop copying at
 * @return the leinght of new string
 *****************************************************************/
stock copyc(String:dest[], len, String:src[], ch)
{
    for(new i=0;i<len-1;++i)
    {
        if(src[i] != ch && src[i] != '\0')
        {    
            dest[i] = src[i]; 
        }
        else
        {
            return i;
        }
    }
    return -1;
}
Olly is offline
Send a message via MSN to Olly
Reply


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 14:51.


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