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