在实际的工作中,需要提取程序中的字符串信息,但是程序中经常将一些数字当做字符串来进行处理,例如表盘的刻度信息,这时候就需要判断字符串是否全为数字,来进行真正意义上的字符串提取。下面介绍了判断字符串是否全为数字的方法,仅供参考。
方法一:判断字符的ASCII范围(数字的范围为48~57)
1 #include <iostream> 2 using namespace std; 3 4 bool AllisNum(string str); 5 6 int main( void ) 7 { 8 9 string str1 = "wolaiqiao23"; 10 string str2 = "1990"; 11 12 if (AllisNum(str1)) 13 { 14 cout<<"str1 is a num"<<endl; 15 } 16 else 17 { 18 cout<<"str1 is not a num"<<endl; 19 } 20 21 if (AllisNum(str2)) 22 { 23 cout<<"str2 is a num"<<endl; 24