判断是不是纯数字字符串

判断一个字符串是不是纯数字字符串

注:1、当字符串首位是0的时候,如“-0123345”或者“01232345”,此处认为是合法的纯数字字符串

  2、当字符串全部是0的时候,如“0000”,此处也认为是合法的纯数字字符串

 1     bool IsAllDigit(const std::string &sConnect)
 2     {
 3         if (sConnect.empty() )
 4         {
 5             return false;
 6         }
 7 
 8         std::string _sConnect = sConnect;
 9         if (_sConnect[0] == '-')
10         {
11             //此处-1是为了不被最后一位结束符影响,可以不-1
12             _sConnect = sConnect.substr(1,sConnect.length() - 1);
13         }
14
15 if (_sConnect.empty() ) 16 { 17 return false; 18 } 19 20 for (size_t i = 0; i < _sConnect.length(); i++) 21 { 22 if (_sConnect[i] < '0' || _sConnect[i] > '9') 23 { 24 return false; 25 } 26 } 27 28 return true; 29 }
原文地址:https://www.cnblogs.com/LYF-LIUDAO/p/7019595.html