searching a string, stripping out substrings.

I have a string, for ex
welcome.to.my.nightmare.alice.cooper
I want to put this string into 6 different variables so break on all .'s
with the following I'm only available to break on the first portion of the string (welcome)
                                for (i=0;i<counter;i++){
            cout << strings[i] << endl;
            j= strings[i].find(period);
            output = strings[i].substr(0,j);
            cout << output << endl;
how do I break on the other periods, there will be 30 other strings so each index of the period will change.
police45sAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
IndrawatiConnect With a Mentor Commented:
This code will get any number of strings with any length:

        string strTmp("welcome.to.my.nightmare.alice.cooper");

      vector<string> vStrings;
      string::size_type iCurDot = -1;
      do
      {
            string::size_type iNextDot = strTmp.find('.', iCurDot+1);
            vStrings.push_back(strTmp.substr(iCurDot+1, iNextDot-iCurDot-1));
            iCurDot = iNextDot;
      } while(iCurDot != string::npos);

      for(int ii = 0; ii < vStrings.size(); ++ii)
      {
            cout << vStrings[ii] << '\n';
      }
0
 
itsmeandnobodyelseCommented:
Or that:

     for (i=0;i<counter;i++)
     {
          cout << strings[i] << endl;
          string::size_t pos = 0;
          string::size_t lpos = 0;      
         
          while ((pos = strings[i].find(period, lpos)) != string::npos)
          {
                output = strings[i].substr(lpos,pos-lpos);
                cout << output << endl;
                lpos = pos + 1;
          }
          // print the last substring as well
         output = strings[i].substr(lpos);
         cout << output << endl;
     }

Regards, Alex

0
 
IndrawatiCommented:
Hmm, they're basically the exact same thing.
0
 
itsmeandnobodyelseCommented:
>>> Hmm, they're basically the exact same thing

No, there are differences: your's compiles, mine has a wrong type string::size_t, should be string::size_type ;-)

Regards, Alex
 
0
All Courses

From novice to tech pro — start learning today.