Created
May 30, 2012 16:35
-
-
Save float-tw/2837484 to your computer and use it in GitHub Desktop.
c++ string split
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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