diff --git a/SimoxUtility/algorithm/string/string_tools.cpp b/SimoxUtility/algorithm/string/string_tools.cpp index 6e2de92448a2b3934d170420e7553288affc0d68..d58391f0a470fbb80b2191d517f70940413293bb 100644 --- a/SimoxUtility/algorithm/string/string_tools.cpp +++ b/SimoxUtility/algorithm/string/string_tools.cpp @@ -176,6 +176,20 @@ bool alg::contains(const std::string& haystack, const std::string& needle) return boost::algorithm::contains(haystack, needle); } +unsigned long alg::count(const std::string& input, const std::string& search) +{ + auto start = 0U; + auto end = input.find(search); + unsigned long num = 0; + while (end != std::string::npos) + { + num++; + start = end + search.length(); + end = input.find(search, start); + } + return num; +} + std::string alg::remove_prefix(const std::string& string, const std::string& prefix) { diff --git a/SimoxUtility/algorithm/string/string_tools.h b/SimoxUtility/algorithm/string/string_tools.h index 73e2ba7fed71712dbb3c48f92435a8d69adcf3bb..656b721fd58e52de34b1c3f18b02d4db4de941f0 100644 --- a/SimoxUtility/algorithm/string/string_tools.h +++ b/SimoxUtility/algorithm/string/string_tools.h @@ -100,6 +100,16 @@ namespace simox::alg return string.find(character) != std::string::npos; } + unsigned long count(const std::string& input, const std::string& search); + inline unsigned long count(const std::string& input, const char* search) + { + return count(input, std::string(search)); + } + inline unsigned long count(const std::string& input, const char search) + { + return count(input, std::to_string(search)); + } + /** * @brief If `string` begins with `prefix`, return `string` without the