108

Possible Duplicate:
How to split a string in C++?

Best way to split a string in C++? The string can be assumed to be composed of words separated by ;

From our guide lines point of view C string functions are not allowed and also Boost is also not allowed to use because of security conecerns open source is not allowed.

The best solution I have right now is:

string str("denmark;sweden;india;us");

Above str should be stored in vector as strings. how can we achieve this?

Thanks for inputs.

Makoto
  • 104,088
  • 27
  • 192
  • 230
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
  • 2
    I don't think this should be marked as a duplicate, the other question promotes elegance over efficiency, which I disagree with and avoided for that very reason. – user2462027 Dec 12 '14 at 07:51
  • 1
    "[HOW TO SPLIT A STRING IN C++](http://www.martinbroadhurst.com/how-to-split-a-string-in-c.html)" lists few nice alternatives. – Petr Vepřek Aug 17 '18 at 10:32

3 Answers3

252

I find std::getline() is often the simplest. The optional delimiter parameter means it's not just for reading "lines":

#include <sstream>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<string> strings;
    istringstream f("denmark;sweden;india;us");
    string s;    
    while (getline(f, s, ';')) {
        cout << s << endl;
        strings.push_back(s);
    }
}
Martin Stone
  • 12,682
  • 2
  • 39
  • 53
  • 12
    Great method! Write carefully though. At first I couldn't compile this ... because I had been using double quotes on the delimiter (getline(f, s, ";")). #FeelingStupid. – Francis Pierot Jun 14 '17 at 13:02
  • Any idea how to split string based on another string? For example, split `"abdecfdcfe"` by the string `"de"` which will return `{"ab", "cfdcfe"}`. Also any way to split string for multiple characters? Like in the above example, the answer will be `{"ab", "cf", "cf"}`. – Rahat Zaman Apr 26 '20 at 00:49
  • Using for-loop as in `for (string s; getline(f, s, ';'); )` is also nice. – yugr Feb 27 '23 at 19:42
22

You could use a string stream and read the elements into the vector.

Here are many different examples...

A copy of one of the examples:

std::vector<std::string> split(const std::string& s, char seperator)
{
   std::vector<std::string> output;

    std::string::size_type prev_pos = 0, pos = 0;

    while((pos = s.find(seperator, pos)) != std::string::npos)
    {
        std::string substring( s.substr(prev_pos, pos-prev_pos) );

        output.push_back(substring);

        prev_pos = ++pos;
    }

    output.push_back(s.substr(prev_pos, pos-prev_pos)); // Last word

    return output;
}
Community
  • 1
  • 1
Fox32
  • 13,126
  • 9
  • 50
  • 71
11

There are several libraries available solving this problem, but the simplest is probably to use Boost Tokenizer:

#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>

typedef boost::tokenizer<boost::char_separator<char> > tokenizer;

std::string str("denmark;sweden;india;us");
boost::char_separator<char> sep(";");
tokenizer tokens(str, sep);

BOOST_FOREACH(std::string const& token, tokens)
{
    std::cout << "<" << *tok_iter << "> " << "\n";
}
hkaiser
  • 11,403
  • 1
  • 30
  • 35