Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting words from string?

Status
Not open for further replies.

clubcav

Technical User
Oct 22, 2003
1
US
I'm having trouble, maybe you can help me.

I have a string full of last names (from customer list), and they are seperated by random spaces. Is there any way to seperate each of the last names and store it into one big string?

I know CIN would work, since it ignores spaces, but how would I do it with strings?

Thanks for your help
 
Use the strtok function to separate out the names.

If you need more information please post an example of your input data.

Cheers - Gavin
 
If you are using the standard string class, use stringstreams.

Code:
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string theString = &quot;Last Names Separated   By    Some Spaces&quot;;
    std::istringstream theStream(theString);
    std::string currentName;
    theStream >> currentName;
    while (!theStream.fail())
    {
        std::cout << currentName << std::endl;
        theStream >> currentName;
    }
}
 
uolj has the right idea, but after doinf similar things over and over again, you may want to turn that into a more genaric tokenize function. Also, uolj, could have ommitted the std::'s if he had justed used the namespace like this:
Code:
#include <string>
using namespace std;

vector<string> tokenize(const string& inst, const string& delims)
{
 vector<string> vecTokens;
 vecTokens.clear();
 size_type firstIndex, lastIndex;
 firstIndex = inst.find_first_not_of(delims);
 while( firstIndex != string::npos )
 {
  lastIndex = inst.find_first_of(delims, firstIndex);
  if( lastIndex == string::npos ) 
    lastIndex = inst.length();
  vecTokens.push_back(inst.substr(firstIndex(lastIndex ->firstIndex) ) );
  firstIndex = inst.find_first_not_of(delims, lastIndex);
 }
 return vecTokens;
}
 
The tokenize method is a good idea.

> Also, uolj, could have ommitted the std::'s if he had justed used the namespace like this:

As far as putting in the using directive, I would only recommend that for beginners implementing an entire program in a single source file. I prefer to use std:: even in simple examples so that people get used to seeing it. Since it is generally bad practice to put the using directive in a header file, I use std:: there. And since I like to be consistent between header and source files, I use std:: in source files also. So basically I think its better to use std:: everywhere, which is why I like to use it in even the simplest examples. :)
 
has a tokenize library.


And global &quot;using namespace xxx;&quot; = bad in header files.

Code:
void function()
{
    using blah;
}

is a nice compromise from using std:: a billion times and not making anyone who includes the header &quot;use&quot; your namespaces.


I see nothing fundamentally wrong with &quot;using namespace xxx&quot; in a source file... but like uolj said, for the sake of consistency, and to keep from polluting the global namespace, it's best not to.

In general, I prefer &quot;using&quot; each identifier at the beginning of each function it's used in.
 
Anyother namespace, and I'd agree, but the std is required for everything, as a practice I just use it in everything and scope any other namsaces...

The boost library is powerful, but it's a little big to download just for tokenizing fuctionality. To be honest, I am waiting for it to become a standard before writting anything big or important with it.
 
My main reason for not &quot;using namespace std&quot; is that you should be able to #include <iostream>, include someone else's header file, and not have to worry about conflicting with some obscure class name buried deep in an iostream header somewhere.

In other words, you should be able to use a piece of a large library without having to know everything about that library, and without getting strange errors because you don't.

Also, even if you do make the decision to &quot;using namespace std&quot;, I think that should be your choice, not the collective choice of the authors of the header files you use.


> The boost library is powerful, but it's a little big to download just for tokenizing fuctionality.

Agreed. One thing I don't like about Boost is that it's not straightforward to just download and use a piece of it. Hopefully they'll make that easier to do sometime in the future.

I think a few pieces of Boost are already on the list of things that will be in the next C++ Standard. Hopefully, more will follow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top