首页 > 其他 > 详细

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

时间:2021-07-19 17:07:16      阅读:15      评论:0      收藏:0      [点我收藏+]
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

原文:https://www.cnblogs.com/Fred1987/p/15030313.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!