From b2becb11ef090a34024c07730a92e371f838c040 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 4 Dec 2020 14:10:24 -0500 Subject: [PATCH] Added function to split a string on a character --- Algorithms.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Algorithms.hpp b/Algorithms.hpp index 38090f0..7296aff 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -517,6 +517,29 @@ int findNumOccurrence(std::string str, char ch){ return num; } +//Return a vector of strings split on the delimiter +std::vector split(std::string str, char delimiter){ + std::vector 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; +} + + }