Locating all instances of a substring : string substr « string « C++ Tutorial






#include <string>
#include <iostream>

int main ()
{
    using namespace std;

    string strSample ("Good day String!");
    size_t nOffset = strSample.find ("day", 0);

    size_t nSubstringOffset = strSample.find ("day", 0);

    while (nSubstringOffset != string::npos)
    {
        cout << "\"day\" found at offset " << nSubstringOffset << endl;
        // Make the 'find' function search the next character onwards
        size_t nSearchOffset = nSubstringOffset + 1;

        nSubstringOffset = strSample.find ("day", nSearchOffset);
    }

    return 0;
}








15.23.string substr
15.23.1.string member function substr
15.23.2.substr 'to-end-of-string' option
15.23.3.Locating all instances of a substring
15.23.4.Split file name in command line into base name and extension
15.23.5.finding substrings in string objects