Jump to content
  • Advertisement
Sign in to follow this  

how to split a string in c++?

This topic is 3315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.


#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);
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
Original post by Storyyeller
Wouldn't that be even more complicated then the StringStream method?

Yeah, you're right. String streams are better for that sort of thing... I tend to not use stringstreams but that's mostly because I'm not nearly as familiar with them as I should be... -_-

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!