Added function to split a string on a character

This commit is contained in:
2020-12-04 14:10:24 -05:00
parent 82c3bd79f0
commit b2becb11ef

View File

@@ -517,6 +517,29 @@ int findNumOccurrence(std::string str, char ch){
return num;
}
//Return a vector of strings split on the delimiter
std::vector<std::string> split(std::string str, char delimiter){
std::vector<std::string> splitStrings;
int location = 0;
location = str.find(delimiter);
while(location != std::string::npos){
//Split the string
std::string firstString = str.substr(0, location);
str = str.substr(location + 1); //+1 to skip the delimiter itself
//Add the string to the vector
splitStrings.push_back(firstString);
//Get the location of the next delimiter
location = str.find(delimiter);
}
//Get the final string if it isn't empty
if(!str.empty()){
splitStrings.push_back(str);
}
//Return the vector of strings
return splitStrings;
}
}