get the tokens from the string
int okutil_get_tokens( LPCSTR lpcszSrc, StringArray * psaResult, char cDelimiter, int * lpErr, int nTrimQuotes = '"', unsigned int dwCntrl = 0 )
the number of tokens
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 */ }
String::GetTokens, String::SetTokens, String::GetNumTokens
origin.h