string::GetTokens

Description

Create a StringArray from this string, this process depend on code page, default is system code page,it can be changed by function set_code_page_for_string_process

The second version, allows for automatic conversion of numeric strings to a double type vector.

Note: If one or more tokens in the string contain a single "double quote" character ("), even escaped like \", this method will output a general operation failure error and code execution will stop at that point. See the last example below for how to use the okutil_get_tokens function to avoid this issue.

Syntax

int GetTokens( StringArray & saTokens, char chDelimiter = 0 )

int GetTokens( vector & vTokens, char chDelimiter = 0 )

Parameters

saTokens
[output]string array which store the results found.
chDelimiter
[input]A character representing the delimiter.

vTokens
[output]vector of doubles to receive the results found.
chDelimiter
[input]A character representing the delimiter.

Return

Number of tokens (strings) found


Number of tokens found

Examples

EX1

void string_GetTokens_ex1()
{
    StringArray strTokens;
    strTokens.SetSize(3);
    string str = "1@2@3#4#5$6$";
    int nRet = str.GetTokens( strTokens, '@');
    if(nRet)
    {
        //Should print "1    2       3#4#5$6$" change '@' to '#' or '$' to see different result
        for(int ii = 0; ii<nRet; ii++)
                printf("%s\t", strTokens[ii]);
    }
    else
        printf("Fail to get token.\n")

}


EX2

void string_GetTokens_ex2()
{
    vector   vTokens;
           
    string str = "1@2@3@7.897";
    int nRet = str.GetTokens( vTokens, '@');
    if(nRet)
    {
        //Should print "1.000000     2.000000        3.000000        7.897000".
        for(int ii = 0; ii<nRet; ii++)
                printf("%f\t", vTokens[ii]);
    }
    else
        printf("Fail to get token.\n")
}

Example for when one or more tokens in the string contain a single double quote character. See Description for more information.

void okutil_get_tokens_ex()
{
        string str = "a|\"b\"|\"|c\"d|e\\\"f";// String: a|"b"|"|c"d|e\"f
        StringArray sa;

        okutil_get_tokens(str, &sa, '|', NULL, 0, GT_IGNORE_QUOTES);

        for( int ii= 0; ii < sa.GetSize(); ii++ )
        {
                printf("%s\n", sa[ii]);
        }
        
        /* Outputs:
                a
                "b"
                "
                c"d
                e\"f
        */
}

Remark

See Also

okutil_get_tokens, string::SetTokens, string::GetNumTokens, string::GetToken

header to Include

origin.h