AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [USEFUL FUNCTION] Explode (https://forums.alliedmods.net/showthread.php?t=55020)

Olly 05-11-2007 08:46

[USEFUL FUNCTION] Explode
 
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;
}



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

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