Skip to content

Instantly share code, notes, and snippets.

@equalent
Forked from float-tw/string_split.cpp
Created October 7, 2018 14:52
Show Gist options
  • Save equalent/b6b0231ac0c4fe9ffa232b6c3340583a to your computer and use it in GitHub Desktop.
Save equalent/b6b0231ac0c4fe9ffa232b6c3340583a to your computer and use it in GitHub Desktop.
c++ string split
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void split(string source, string delim, vector<string>& result)
{
string tmp;
size_t now=-1, next=-1;
result.clear();
while(true)
{
now = next+1;
next = source.find_first_of(delim, now);
if( string::npos == next ) break;
if( 0 == next - now ) continue;
result.push_back( source.substr(now, next - now) );
}
if( now != source.size() )
result.push_back( source.substr(now) );
return;
}
int main()
{
string s;
vector<string> r;
s = ",,,abc,def gh,i,,,,j k;,,,,";
split(s, ",", r);
for(int i=0; i<r.size(); i++)
cout << r[i] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment