Solved

searching a string, stripping out substrings.

Posted on 2004-10-27
196 Views
Last Modified: 2010-04-01
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.
0
Question by:police45s
    4 Comments
     
    LVL 3

    Accepted Solution

    by:
    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
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    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
     
    LVL 3

    Expert Comment

    by:Indrawati
    Hmm, they're basically the exact same thing.
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    >>> 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Some Windows API functions expect you to provide a pointer to a CALLBACK function that the system will need to call as part of the operation.  Such API functions as SetTimer, timeSetEvent, CreateThread, EnumWindows, LineDDA, even window message hand…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now