I've made a splitString() function which receives a character and a string and returns a vector<string> containing all the string split by a character in the input string:
vector<string> splitString(string input, char separator) {
size_t pos;
vector<string> text;
while (!input.empty()) {
pos = input.find(separator); //find separator character position
if (pos == string::npos) {
text.push_back(input);
break;
}
text.push_back(input.substr(0, pos));
input = input.substr(pos + 1);
}
return text; //returns a vector with all the strings
}
Example Input(.csv):
name;surname;age
Output would be:
vector<string> contains {name, surname, age}
Is there any efficient way of doing this?
As you can see, the ; was deleted separating the strings.
I was wondering if there is any way of adding an option to NOT delete the separator character, including it in the split strings.