forked from L1nkus/split
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.cpp
More file actions
23 lines (22 loc) · 677 Bytes
/
split.cpp
File metadata and controls
23 lines (22 loc) · 677 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <string>
#include "main.h"
std::vector<std::string> split(std::string str, char splitter){
std::vector<int> semicolons;
std::vector<std::string> tokens;
for(size_t i=0;i<str.length();i++){
if(str[i]==splitter){
semicolons.push_back(i);
}
}
for(size_t i=0;i<semicolons.size()+1;i++){
if(i==0)
tokens.push_back(str.substr(0, semicolons[i]));
else if(i==semicolons.size()){
tokens.push_back(str.substr(semicolons[i-1]+1));
}
else
tokens.push_back(str.substr(semicolons[i-1]+1, semicolons[i]-semicolons[i-1]-1));
}
return tokens;
}