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

前端 未结 6 1560
日久生厌
日久生厌 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: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 >( locale() ).scan_is( ctype::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.

提交回复
热议问题