Skip to content
Snippets Groups Projects
Commit bd831df8 authored by Fabian Tërnava's avatar Fabian Tërnava
Browse files

added string count method

parent 22376fb9
No related branches found
No related tags found
No related merge requests found
......@@ -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)
{
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment