mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added function to split a string on a character
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user