2.1.2.37 okutil_get_tokens


Description

get the tokens from the string

Syntax

int okutil_get_tokens( LPCSTR lpcszSrc, StringArray * psaResult, char cDelimiter, int * lpErr, int nTrimQuotes = '"', unsigned int dwCntrl = 0 )

Parameters

lpcszSrc
[input] the source string to get tokens
psaResult
[output] the array that store the tokens get from lpcszSrc
cDelimiter
[input] the delimiter between tokens. = 0 for all while space delimiter, such as \t, \n, ' '
lpErr
[output] it return 0 if success
nTrimQuotes
Only useful when cDelimiter = 0.
can be assigned as " character or GT_TRIM_QUOTES(-1) to trim quotes.
or assigned as GT_TRIM_BOTH_ENDS(-2) to trim while space from both ends.
dwCntrl
default is 0, or can be GT_IGNORE_QUOTES, used to treate quoted string as a single token. GT_IGNORE_QUOTES can only be used together with nTrimQuotes as the " character.

Return

the number of tokens

Examples

EX1

void okutil_get_tokens_ex1()
{
    //get the tokens from a string, Delimiter is ','
    string str = "a,b,c";
    StringArray sa;
    
    //if we do not want to get the return state, set lpErr = NULL
    int n = okutil_get_tokens(str, &sa, ',', NULL);
    
    printf("We get %d tokens in total!\n", n);
    //output the tokens we get
    for( int ii = 0; ii < n; ii++ )
    {
        out_str(sa[ii]);
    }
}

EX2

//there is a pair of quotes in str string, treated quotes as whole
void okutil_get_tokens_ex2()
{    
    string 		str = "a\tb \"1 2 3\" c";    
    StringArray sa;
    
    char 		cDelimiter = 0; // 0 means all while space, like \t, \n, ' '.    
    int 		nTrimQuotes = '"'; // to trim quotes, or use GT_TRIM_QUOTES for same effect.
    unsigned int nCntrl = GT_IGNORE_QUOTES; // to ignore the while space inside quotes, so treated quotes as whole
    
    int num = okutil_get_tokens(str, &sa, cDelimiter, NULL, nTrimQuotes, nCntrl);
    
    printf("We get %d tokens in total!\n", num); // should get 4 tokens    
    for( int ii = 0; ii < num; ii++ )
    {
        out_str(sa[ii]);
    }
    /*
    Output should be:
    	a
    	b
    	1 2 3
    	c
    */
}

EX3

// seperate by while space, and trim all while space for each token both ends.
void okutil_get_tokens_ex3()
{    
    string 		str = "a b\nc\td";    
    StringArray sa;
    char 		cDelimiter = 0; // 0 means all while space, like \t, \n, ' '.
    int 		nTrimQuotes = GT_TRIM_BOTH_ENDS; // to trim while space from both ends
    int num = okutil_get_tokens(str, &sa, cDelimiter, NULL, nTrimQuotes);
    
    printf("We get %d tokens in total!\n", num); // should get 4 tokens    
    for( int ii = 0; ii < num; ii++ )
    {
        out_str(sa[ii]);
    }   
    /*
    Output should be:
    	a
    	b
    	c
    	d
    */    
}

Remark

See Also

String::GetTokens, String::SetTokens, String::GetNumTokens

Header to Include

origin.h

Reference