From 25bd26972945f956ad4c92ce8a61bb6126c0dedb Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sun, 13 Mar 2022 15:52:53 +0100 Subject: Implementation Split --- Src/StringTools.cpp | 28 ++++++++++++++++++++++++++++ Src/StringTools.h | 34 +++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 15 deletions(-) (limited to 'Src') diff --git a/Src/StringTools.cpp b/Src/StringTools.cpp index 931bd75..302fad9 100644 --- a/Src/StringTools.cpp +++ b/Src/StringTools.cpp @@ -92,3 +92,31 @@ std::string StringTools::Upper(const std::string& str) { return ss.str(); } + +std::vector StringTools::Split(const std::string& str, const std::string& seperator) { + std::vector toRet; + + // Quick-accept: seperator length is 0 + if (seperator.length() == 0) { + for (const char c : str) + toRet.push_back(std::string(&c, (&c) + 1)); + } + + else { + std::size_t idx = 0; + while (idx != std::string::npos) { + std::size_t lastIdx = idx; + idx = str.find(seperator, idx + seperator.length()); + + toRet.push_back(str.substr( + lastIdx, + idx - lastIdx + )); + + if (idx != std::string::npos) + idx += seperator.length(); + } + } + + return toRet; +} diff --git a/Src/StringTools.h b/Src/StringTools.h index 0c08805..d65772a 100644 --- a/Src/StringTools.h +++ b/Src/StringTools.h @@ -2,32 +2,36 @@ #define STRINGTOOLS_STRINGTOOLS_H #include +#include /* Handy utensils to manipulate strings */ class StringTools { public: - //! Will replace every occurence of `find` in `str` by `subst`. - static std::string Replace(const std::string& str, const char find, const std::string& subst); + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const char find, const std::string& subst); - //! Will replace every occurence of `find` in `str` by `subst`. - static std::string Replace(const std::string& str, const std::string& find, const std::string& subst); - - //! Will replace every occurence of `find` in `str` by `subst`. - static std::string Replace(const std::string& str, const char find, const char subst); + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const std::string& find, const std::string& subst); - //! Will replace every occurence of `find` in `str` by `subst`. - static std::string Replace(const std::string& str, const std::string& find, const char subst); + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const char find, const char subst); - //! Will make a string all-lowercase. - static std::string Lower(const std::string& str); + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const std::string& find, const char subst); - //! Will make a string all-uppercase. - static std::string Upper(const std::string& str); + //! Will make a string all-lowercase. + static std::string Lower(const std::string& str); + + //! Will make a string all-uppercase. + static std::string Upper(const std::string& str); + + //! Will split a string by a string seperator + static std::vector Split(const std::string& str, const std::string& seperator); private: - // No instanciation! >:( - StringTools(); + // No instanciation! >:( + StringTools(); }; #endif //STRINGTOOLS_STRINGTOOLS_H -- cgit v1.2.3