VC++如何判断一个字符串(CString)是否有全为数字,如果全为数字就用MessageBox提示
void CheckString( CString str )
{
BOOL bIsDigit = TRUE;
int nCount = str.GetLength(); // 获得字符个数
for ( int i = 0; i < nCount; i ++ )
{
if ( 0 == isdigit( str.GetAt(i) ) ) // 不是数字就置标志位
{
bIsDigit = FALSE;
break;// 退出
}
}
if ( bIsDigit ) //如果始终是,那么表示全是数字,给予提示
{
::MessageBox( NULL, TEXT("Is Digit"), NULL, NULL );
}
}
{
BOOL bIsDigit = TRUE;
int nCount = str.GetLength(); // 获得字符个数
for ( int i = 0; i < nCount; i ++ )
{
if ( 0 == isdigit( str.GetAt(i) ) ) // 不是数字就置标志位
{
bIsDigit = FALSE;
break;// 退出
}
}
if ( bIsDigit ) //如果始终是,那么表示全是数字,给予提示
{
::MessageBox( NULL, TEXT("Is Digit"), NULL, NULL );
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2008-06-15
如果确认只有数字和字母则可以如下判断
BOOL CheckString(CString strLIne)
{
CString strTemp = strLIne;
strTemp.MakeLower();
strLIne.MakeUpper();
if(strLIne == strTemp) return TRUE;
return FALSE;
}
BOOL CheckString(CString strLIne)
{
CString strTemp = strLIne;
strTemp.MakeLower();
strLIne.MakeUpper();
if(strLIne == strTemp) return TRUE;
return FALSE;
}