Add is_numeric Method

This commit is contained in:
2020-05-01 11:12:54 +02:00
parent 639a0a87f9
commit 166999290b
2 changed files with 15 additions and 2 deletions
+13 -2
View File
@@ -8,7 +8,7 @@
namespace StringAlgorithm { namespace StringAlgorithm {
namespace Internal { 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); size_t found_pos = haystack.find(needle, pos);
if (0 == nth || std::string::npos == found_pos) if (0 == nth || std::string::npos == found_pos)
@@ -84,7 +84,7 @@ std::vector<std::string> split(const std::string& subject, const std::string del
{ {
std::string s(subject); std::string s(subject);
char delimiter = delimiters.front(); char delimiter = delimiters.front();
for (const char& c : delimiters) for (const char& c : delimiters)
std::replace(s.begin(), s.end(), c, delimiter); 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; 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 } // namespace StringAlgorithm
+2
View File
@@ -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); 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 } // namespace StringAlgorithm
#endif // STRINGALGORITHM_H #endif // STRINGALGORITHM_H