Last active
April 13, 2020 00:07
-
-
Save luqiang21/d9c5aef9c951355c2503de682b75190f to your computer and use it in GitHub Desktop.
c++ string split
This file contains 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 <string> | |
#include <vector> | |
void split(vector<string>& splitted, string& pattern, char delimiter) { | |
if (pattern.size() == 0) { | |
splitted.push_back(pattern); | |
return; | |
} | |
int start = 0, i; | |
for (i = 0; i < pattern.size(); ++i) { | |
char ch = pattern[i]; | |
if (ch == delimiter) { | |
splitted.push_back(pattern.substr(start, i - start)); | |
start = i + 1; | |
} | |
} | |
if (start == 0) { | |
splitted.push_back(pattern); | |
return; | |
} | |
splitted.push_back(pattern.substr(start, i + 1 - start)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment