Skip to content

Instantly share code, notes, and snippets.

@abrarShariar
Created January 3, 2016 19:14
Show Gist options
  • Save abrarShariar/c5344b0cbc1f681da0ce to your computer and use it in GitHub Desktop.
Save abrarShariar/c5344b0cbc1f681da0ce to your computer and use it in GitHub Desktop.
Code snippet to split a string similar to explode() in PHP
/*
This code snippet is collected from http://www.cplusplus.com/articles/2wA0RXSz/
*/
#include<iostream>
#include<vector>
using namespace std;
const vector<string> explode(const string& s, const char& c)
{
string buff{""};
vector<string> v;
for(auto n:s)
{
if(n != c) buff+=n; else
if(n == c && buff != "") { v.push_back(buff); buff = ""; }
}
if(buff != "") v.push_back(buff);
return v;
}
int main()
{
string str{"the quick brown fox jumps over the lazy dog"};
vector<string> v{explode(str, ' ')};
for(auto n:v) cout << n << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment