From daef10e3244c5380a257334bab2be1736875e572 Mon Sep 17 00:00:00 2001 From: rinkk <karsten.rink@ufz.de> Date: Tue, 9 Aug 2016 17:36:07 +0200 Subject: [PATCH] added alternate method for deconstucting string into a vector of words --- BaseLib/StringTools.cpp | 10 ++++++++++ BaseLib/StringTools.h | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/BaseLib/StringTools.cpp b/BaseLib/StringTools.cpp index 67694745465..24e14a82f7d 100644 --- a/BaseLib/StringTools.cpp +++ b/BaseLib/StringTools.cpp @@ -25,6 +25,16 @@ namespace BaseLib { +std::vector<std::string> splitString(std::string const& str) +{ + std::istringstream str_stream(str); + std::vector<std::string> items; + std::copy(std::istream_iterator<std::string>(str_stream), + std::istream_iterator<std::string>(), + std::back_inserter(items)); + return items; +} + std::list<std::string> splitString(const std::string &str, char delim) { std::list<std::string> strList; diff --git a/BaseLib/StringTools.h b/BaseLib/StringTools.h index 7ff25e9b5f4..64ad1be5192 100644 --- a/BaseLib/StringTools.h +++ b/BaseLib/StringTools.h @@ -19,9 +19,18 @@ #include <string> #include <list> #include <sstream> +#include <vector> namespace BaseLib { +/** + * Splits a string into a vector of strings. This method only works for string seperation + * recognised by the std::stringstream iterator such as ' ' or '\t'. + * \param str String to be splitted + * \return Vector of strings + */ +std::vector<std::string> splitString(std::string const& str); + /** * Splits a string into a list of strings. * \param str String to be splitted -- GitLab