From 5bc3335d541f53570cf59d3ebc2fc69089f8405b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 15 Jan 2020 12:30:49 +0100 Subject: [PATCH] string: adding strjoin() function --- src/string.cpp | 15 +++++++++++++++ src/string.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/src/string.cpp b/src/string.cpp index 89ff7b9..d5af6f8 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -46,6 +46,21 @@ strlist_t strsplit(const std::string& str, const std::string& delim) { return r; } +std::string strjoin(const strlist_t& list, const std::string& delim) { + + std::string out; + + for(const std::string& item : list) { + out += item + delim; + } + + if (out.length() > 0) { + out.erase(out.end() - delim.length()); + } + + return out; +} + std::string& strtolower(std::string& str) { std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::tolower(c); }); return str; diff --git a/src/string.h b/src/string.h index 0358667..98c1c8a 100644 --- a/src/string.h +++ b/src/string.h @@ -33,6 +33,8 @@ strlist_t strsplitwords(const std::string& str, const std::string& delim = ","); strlist_t strsplit(const std::string& str, const std::string& delim); +std::string strjoin(const strlist_t& list, const std::string& delim); + std::string& strtolower(std::string& str); std::string& rtrim(std::string& str);