Sign in to follow this  
Storyyeller

how to split a string in c++?

Recommended Posts

Suppose I have two vectors of strings and a large string. The large string contains a comma separated list of names followed by :: followed by another list of comma separated names. If I wanted to store the first list of names in the first vector and the second list of names in the other vector, how should I go about doing it? I thought about using an IStringStream and repeatedly calling GetLine, but I figured there was probably a better way to do it that I didn't know about.

Share this post


Link to post
Share on other sites
Seeing as you mentioned vector's I can only assume that you're using std::strings. That being the case, that's actually very easy.

I'm not going to give you the exact code because that wouldn't be doing you any favors at all (you need to learn after all... ;) ) but I will point you in the right direction.

First, you should take a good look at this site. It's a great reference about string and its various functions.

There are several ways to do this. You can use the substr() function to break your string into 'tokens'. In order to know where to break it apart, you can use the find() function to build a temporary list of the positions where the commas are in your primary string and then iterate through that list generating substrings.

Hope this helps. Good luck!

Share this post


Link to post
Share on other sites
There's boost tokenizor and boost string algorithm.

Example:


#include <iostream>
#include <string>
#include <vector>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main()
{
// here's a string in the format you described, plus
// two empty vectors v1 and v2
string text = "A,B,C::D,E,F";
vector<string> v1;
vector<string> v2;

// tokenize the string by the "::" character
// and get an iterator to the first tokenizer result
char_separator<char> sep("::");
tokenizer<char_separator<char>> tokens(text, sep);
tokenizer<char_separator<char>>::iterator it = tokens.begin();

// split (defined in algorithm/string.hpp will tokenize
// the string pointed to by it and store each token in
// the specified container
split(v1, string(*it), is_any_of(","), token_compress_on);
++it;
split(v2, string(*it), is_any_of(","), token_compress_on);
}





Edit: Added links.

Share this post


Link to post
Share on other sites
I would just use the stringstream approach, to be honest. If you were reading whitespace-delimited words, you could do some fancy stuff with the standard library algorithm 'std::copy' and iterator adapter 'std::istream_iterator', but as it is, it'd be much more work to get something similar set up, versus writing the loop yourself.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this