How to find out if a character in a string is an integer

Let's say I want to look at the character at position 10 on line s.

s.at (10);

What would be the easiest way to find out if this is a number?

+4
source share
5 answers

Use isdigit

std::string s("mystring is the best"); if ( isdigit(s.at(10)) ){ //the char at position 10 is a digit } 

You will need

 #include <ctype.h> 

to ensure that isdigit is available regardless of the implementation of the standard library.

+11
source

The rest of the answers suggest that you only need the following characters: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 . If you are writing software that can run on locales using other number systems, then you will want to use the new std::isdigit located at <locale> : http://www.cplusplus.com/reference/std/locale/ isdigit /

Then you can recognize the following numbers as numbers: เฅช, เฉฌ, เตฆ, เฏซ, เน“, เป’

+8
source

Below you will be told:

 isdigit( s.at( 10 ) ) 

true will be allowed if the character at position 10 is a digit.

You need to include <ctype>.

+5
source

Use isdigit :

 if (isdigit(s.at(10)) { ... } 
0
source

Another way is to check the ASCII value of this character

 if ( s.at(10) >= '0' && s.at(10) <= '9' ) // it a digit 
0
source

Source: https://habr.com/ru/post/1302465/


All Articles