[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

searching a string, stripping out substrings.

Posted on 2004-10-27
7
Medium Priority
?
206 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
Comment
Question by:police45s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 3

Accepted Solution

by:
Indrawati earned 500 total points
ID: 12430471
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
ID: 12430631
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
ID: 12430650
Hmm, they're basically the exact same thing.
0
 
LVL 39

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

656 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