diff --git a/src/strutils.cpp b/src/strutils.cpp index b8359c9..b26f363 100644 --- a/src/strutils.cpp +++ b/src/strutils.cpp @@ -115,4 +115,20 @@ std::string stringReplaceAll(std::string_view search, std::string_view replace, } return str; } + +std::optional getStringBetween(std::string_view search, std::string_view prefix, std::string_view suffix) +{ + auto beginIndex = search.find(prefix); + if (beginIndex == std::string_view::npos) + return std::nullopt; + + auto restView = search.substr(beginIndex + prefix.size()); + + auto endIndex = restView.find(suffix); + if (endIndex == std::string_view::npos) + return std::nullopt; + + return restView.substr(0, endIndex); +} + } // namespace cpputils diff --git a/src/strutils.h b/src/strutils.h index 6eb474b..64d74bd 100644 --- a/src/strutils.h +++ b/src/strutils.h @@ -47,6 +47,8 @@ std::string stringReplaceAll(char search, std::string_view replace, std::string_ //std::string stringReplaceAll(std::string_view search, char replace, std::string_view subject); std::string stringReplaceAll(std::string_view search, std::string_view replace, std::string_view subject); +std::optional getStringBetween(std::string_view search, std::string_view prefix, std::string_view suffix); + constexpr const std::string_view allDigitsAndCharacters = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"