From 166999290b91ee38837c7239c5ea221d21021b2c Mon Sep 17 00:00:00 2001 From: JDierkse Date: Fri, 1 May 2020 11:12:54 +0200 Subject: [PATCH] Add is_numeric Method --- Utilities/StringAlgorithm.cpp | 15 +++++++++++++-- include/StringAlgorithm.h | 2 ++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Utilities/StringAlgorithm.cpp b/Utilities/StringAlgorithm.cpp index 33c40b4..c1c9488 100644 --- a/Utilities/StringAlgorithm.cpp +++ b/Utilities/StringAlgorithm.cpp @@ -8,7 +8,7 @@ namespace StringAlgorithm { namespace Internal { -size_t find_nth(const std::string& haystack, size_t pos, const std::string& needle, size_t nth) +size_t find_nth(const std::string& haystack, size_t pos, const std::string& needle, size_t nth) { size_t found_pos = haystack.find(needle, pos); if (0 == nth || std::string::npos == found_pos) @@ -84,7 +84,7 @@ std::vector split(const std::string& subject, const std::string del { std::string s(subject); char delimiter = delimiters.front(); - + for (const char& c : delimiters) std::replace(s.begin(), s.end(), c, delimiter); @@ -118,4 +118,15 @@ std::string::const_iterator find_nth(const std::string& haystack, const std::str return iterator; } +bool is_numeric(const std::string& string) +{ + auto result = double(); + std::stringstream i; + + i << string; + i >> result; + + return !i.fail() && i.eof(); +} + } // namespace StringAlgorithm diff --git a/include/StringAlgorithm.h b/include/StringAlgorithm.h index 7e5825c..fda31a6 100644 --- a/include/StringAlgorithm.h +++ b/include/StringAlgorithm.h @@ -31,6 +31,8 @@ bool icontains(const std::string& subject, const std::string& find); std::string::const_iterator find_nth(const std::string& haystack, const std::string& needle, size_t nth); +bool is_numeric(const std::string& str); + } // namespace StringAlgorithm #endif // STRINGALGORITHM_H