I want to know if a string has any digits, or if there are no digits. Is there a function that easily does this?
+9
A:
Perhaps the following:
if (std::string::npos != s.find_first_of("0123456789"))
{
std::cout << "digit(s)found!" << std::endl;
}
Beh Tou Cheh
2010-02-27 07:56:55
A:
given std::String s;
if( s.find_first_of("0123456789")!=std::string::npos )
//digits
KitsuneYMG
2010-02-27 07:58:08
+1
A:
boost::regex re("[0-9]");
const std::string src = "test 123 test";
boost::match_results<std::string::const_iterator> what;
bool search_result =
boost::regex_search(src.begin(), src.end(), what, re, boost::match_default);
skwllsp
2010-02-27 07:58:20
Or with tr1: http://codepad.org/2TZD5yF5
Manuel
2010-02-27 08:49:36
+1
A:
find_first_of
is probably your best bet, but I've been playing around with iostream facets so here's an alternative:
if ( use_facet< ctype<char> >( locale() ).scan_is( ctype<char>::digit,
str.data(), str.data() + str.size() ) != str.data + str.size() )
Change string
to wstring
and char
to wchar
and you might theoretically have a chance at handling those weird fixed-width digits used in some Asian scripts.
Potatoswatter
2010-02-27 08:29:24
kewl, an upvote. Unfortunately, I tried this later on (in Mac OS X, which has somewhat lackluster locales) and fullwidth digits turned out to be non-numeric.
Potatoswatter
2010-05-12 03:35:17
A:
#include <cctype>
#include <algorithm>
#include <string>
if (std::find_if(s.begin(), s.end(), (int(*)(int))std::isdigit) != s.end())
{
// contains digit
}
newacct
2010-02-27 08:32:54
It's weird, in VC++ the cast is not necessary, and in GNU it's not necessary either if you remove the `std::` part before `isdigit`. Anybody knows why this is so?
Manuel
2010-02-27 09:02:14
@Manuel: `<cctype>` includes the Unix/C `<ctype.h>` and imports its contents with `using`, on Mac OS X at least. §17.4.1.2/4 would seem to say that this methodology is illegal, and `<cctype>` shouldn't define `::isdigit`. Could it be intentional nonconformance?
Potatoswatter
2010-02-27 10:06:35
@Potatoswatter - VSC++ 2008 does the same thing, not only for `ctype` but for the entire C std library. Anyway this does not answer why the version with `std::` in front requires a cast.
Manuel
2010-02-27 10:14:43
It's because the `isdigit` from `<cctype>` is a function with type `int(*)(int)` already. `std::isdigit` is a template, so without the cast it's ambiguous. It's in `<locale>`, but I expect you're picking it up from `<string>`. So once `<cctype>` has done its thing, which we're not sure is conforming but is certainly common, `::isdigit` is unambiguously a particular function, whereas `std::isdigit` is overloaded as the function from `<cctype>` plus the template from `<locale>`. None of the template's instantiations has return type `int`, but without the cast they can't be ruled out.
Steve Jessop
2010-02-27 10:43:55
@Steve - Thanks for the great explanation, I had forgotten about `locale`
Manuel
2010-02-27 11:31:31
A:
There's nothing standard for the purpose, but it's not difficult to make one:
template <typename CharT>
bool has_digits(std::basic_string<CharT> &input)
{
typedef typename std::basic_string<CharT>::iterator IteratorType;
IteratorType it =
std::find_if(input.begin(), input.end(),
std::tr1::bind(std::isdigit<CharT>,
std::tr1::placeholders::_1,
std::locale()));
return it != input.end();
}
And you can use it like so:
std::string str("abcde123xyz");
printf("Has digits: %s\n", has_digits(str) ? "yes" : "no");
Edit:
Or even better version (for it can work with any container and with both const and non-const containers):
template <typename InputIterator>
bool has_digits(InputIterator first, InputIterator last)
{
typedef typename InputIterator::value_type CharT;
InputIterator it =
std::find_if(first, last,
std::tr1::bind(std::isdigit<CharT>,
std::tr1::placeholders::_1,
std::locale()));
return it != last;
}
And this one you can use like:
const std::string str("abcde123xyz");
printf("Has digits: %s\n", has_digits(str.begin(), str.end()) ? "yes" : "no");
Dmitry
2010-02-27 10:40:44