如何测试一个CString类型字串是不是数字?怎么样实现?

解决方案 »

  1.   

    应该是测试里面是否包含数字吧?一个一个找,测试ascii码
      

  2.   

    数字的ascii码数值在一定范围内的。CString kk,int a=kk.GetLenghth();
    for(int i=0;i<a;i++)
    {
    Test(kk.Left(i));
    }
    test ()实现每一个字符的判断
      

  3.   

    可以使用函数:IsCharAlphaNumeric(TCHAR ch)判断是否是数字和英文字符或者使用IsCharAlpha(TCHAR ch)判断是否是英文字符。
    微软文档如下:
    IsCharAlpha Function--------------------------------------------------------------------------------The IsCharAlpha function determines whether a character is an alphabetic character. This determination is based on the semantics of the language selected by the user during setup or through Control Panel. SyntaxBOOL IsCharAlpha(          TCHAR ch
    );
    Parametersch
    [in] Specifies the character to be tested. 
    Return ValueIf the character is alphabetic, the return value is nonzero.If the character is not alphabetic, the return value is zero. To get extended error information, call GetLastError. 
    ResWindows 95/98/Me: IsCharAlphaW is supported by the Microsoft&reg; Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.Function InformationHeader Declared in Winuser.h, include Windows.h 
    Import library User32.lib 
    Minimum operating systems Windows 95, Windows NT 3.1 
    Unicode Implemented as Unicode and ANSI versions on Windows NT, Windows 2000, Windows XP See AlsoStrings Overview, IsCharAlphaNumeric
      

  4.   

    在<ctype.h>中有isdigit函数!
      

  5.   

    bool IsNumber( LPCTSTR pszText )
    {
    ASSERT_VALID_STRING( pszText ); for( int i = 0; i < lstrlen( pszText ); i++ )
    if( !_istdigit( pszText[ i ] ) )
    return false; return true;
    }