How to test if a string contains any digits in C++

前端 未结 6 1551
日久生厌
日久生厌 2020-12-11 05:49

I want to know if a string has any digits, or if there are no digits. Is there a function that easily does this?

相关标签:
6条回答
  • 2020-12-11 06:22

    given std::String s;

    if( s.find_first_of("0123456789")!=std::string::npos )
    //digits
    
    0 讨论(0)
  • 2020-12-11 06:25

    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.

    0 讨论(0)
  • 2020-12-11 06:36
    #include <cctype>
    #include <algorithm>
    #include <string>
    
    if (std::find_if(s.begin(), s.end(), (int(*)(int))std::isdigit) != s.end())
    {
      // contains digit
    }
    
    0 讨论(0)
  • 2020-12-11 06:36

    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");
    
    0 讨论(0)
  • 2020-12-11 06:40
    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);
    
    0 讨论(0)
  • 2020-12-11 06:45

    Perhaps the following:

    if (std::string::npos != s.find_first_of("0123456789")) {
      std::cout << "digit(s)found!" << std::endl;
    }
    
    0 讨论(0)
提交回复
热议问题