Link to home
Start Free TrialLog in
Avatar of police45s
police45s

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Indrawati
Indrawati

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of Indrawati
Indrawati

Hmm, they're basically the exact same thing.
>>> 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