判断一个字符串是否是合法数字串

//判断字符串数据是否合法

需要判断非法字符('-'、‘.' 除外)同时还需判断’-‘、’.' 在字符串中的位置, 进而判断是否合法.

BOOL IsNum(const CString str)
{
  int mCount = 0; //'-'个数
  int dCount = 0; //'.'个数
  int nLen = str.GetLength();
  for(int i = 0;i < nLen;i++)
  {
if( str.GetAt(i)=='-')
   mCount++;
if( mCount > 1|| dCount > 1)
return false;
   
if(!(str.GetAt(i) >='0'&&str.GetAt(i) <='9' ))
{
  if( str.GetAt(i)=='.'&&( i>0 && i<str.GetLength()-1))
  {
 dCount++;
 continue;
  }
  else if((str.GetAt(i)=='-')&&(0==i&&str.GetAt(i+1)!='.'))
 continue;
  else
 return false;
}
    
   }
  return true;
}

猜你喜欢

转载自blog.csdn.net/yaowang107/article/details/78417231