• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Split string via delimiter and print the split string and delimiter's index

开发技术 开发技术 4小时前 3次浏览
void testStringFindAndSubstrDemo()
{
    string str = "hello,world,hello,c++,hello,boost,   ,hello,cpp,,hellocpp,,,,,,,,HellostrFind";
    string delim = ",";
    stringFindDemo10(str, delim);
}

void stringFindDemo10(string &str,string &delim)
{ 
    int strLength = str.length();
    if (strLength <= 0)
    {
        return;
    }
    
    int start = 0, findIndex = -1;
    while (1)
    {
        findIndex = str.find(delim, start);
        if (findIndex >= 0 && findIndex <= strLength)
        {
            string result = str.substr(start, findIndex-start);
            if (!result.empty())
            {
                cout << result << "," << findIndex << endl;
            } 
            start = findIndex + 1;
        }
        else
        {
            break;
        }
    }
    cout << str.substr(start)<<","<<start << endl;
}

Split string via delimiter and print the split string and delimiter's index

 


喜欢 (0)