Yac*_*oby 13
使用isdigit
std::string s("mystring is the best");
if ( isdigit(s.at(10)) ){
//the char at position 10 is a digit
}
Run Code Online (Sandbox Code Playgroud)
你会需要
#include <ctype.h>
Run Code Online (Sandbox Code Playgroud)
isdigit无论标准库的实现如何,都可以确保可用.
其他答案假设您只关心以下字符:0, 1, 2, 3, 4, 5, 6, 7, 8, 9.如果您正在编写可能在使用其他数字系统的语言环境中运行的软件,那么您将需要使用std::isdigit位于以下位置的新软件<locale>:http://www.cplusplus.com/reference/std/locale/isdigit/
然后您可以将以下数字识别为数字: ?, ?, ?, ?, ?, ?
以下内容将告诉您:
isdigit( s.at( 10 ) )
Run Code Online (Sandbox Code Playgroud)
如果位置10的字符是数字,则将解析为"true".
您需要包含<ctype>.