27

Possible Duplicate:
Splitting a string in C++

In PHP, the explode() function will take a string and chop it up into an array separating each element by a specified delimiter.

Is there an equivalent function in C++?

5
  • 1
    No, but it's easy enough to write your own implementation. Commented Oct 19, 2012 at 3:33
  • 2
    boost::split from [boost/algorithm/string.hpp](www.boost.org/doc/html/string_algo.html) Commented Oct 19, 2012 at 3:33
  • @KerrekSB I think you should make that an answer before this is closed Commented Oct 19, 2012 at 3:35
  • There's a question titled something like "Splitting a string in C++". It has some good solutions. Commented Oct 19, 2012 at 3:36
  • the answers below have several differences from php's explode(), here is a closer-to-php port Commented Jan 26, 2019 at 11:05

2 Answers 2

37

Here's a simple example implementation:

#include <string> #include <vector> #include <sstream> #include <utility> std::vector<std::string> explode(std::string const & s, char delim) { std::vector<std::string> result; std::istringstream iss(s); for (std::string token; std::getline(iss, token, delim); ) { result.push_back(std::move(token)); } return result; } 

Usage:

auto v = explode("hello world foo bar", ' '); 

Note: @Jerry's idea of writing to an output iterator is more idiomatic for C++. In fact, you can provide both; an output-iterator template and a wrapper that produces a vector, for maximum flexibility.

Note 2: If you want to skip empty tokens, add if (!token.empty()).

Sign up to request clarification or add additional context in comments.

8 Comments

What is the std::move doing in this case? Is it necessary? I compiled without it since I'm not using C++11, and it does just fine. But what is the purpose in this case?
@user1944429: The move avoids copying the string data. Since you have no more use for it inside the loop, it makes sense for the vector to "steal" that data directly without copying it.
it misses a case, in the input scenario of "a,b,c,d," it should return 5 values including the last null, but it do not
if need that here what i used, explode_cpp
@AlokSaini: True, the present code has "optional trailing delimiter" semantics rather than what you wanted. The easiest way to fix this would be to add a check if s.back() == delim and emit another empty string in that case.
|
13

The standard library doesn't include a direct equivalent, but it's a fairly easy one to write. Being C++, you don't normally want to write specifically to an array though -- rather, you'd typically want to write the output to an iterator, so it can go to an array, vector, stream, etc. That would give something on this general order:

template <class OutIt> void explode(std::string const &input, char sep, OutIt output) { std::istringstream buffer(input); std::string temp; while (std::getline(buffer, temp, sep)) *output++ = temp; } 

4 Comments

I believe there is an error in the std::getline call. Where you have input I believe it should be temp
@AntonioCS: Oops, quite right. Thank you!
How would you handle all white space as a separator?
@pacmaninbw: One obvious possibility would be to change the while loop to while (buffer >> temp).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.