8

ファイルから読み取り、その内容を解析しています。CString値が数字のみで構成されていることを確認する必要があります。私がそれを達成できるさまざまな方法は何ですか?

サンプルコード:

Cstring Validate(CString str)
{
  if(/*condition to check whether its all numeric data in the string*/)
  {
     return " string only numeric characters";
  }
  else
  {
     return "string contains non numeric characters";
  }
}
4

2 に答える 2

19

isdigitすべての文字を反復処理し、文字が数値かどうかを関数で確認できます。

#include <cctype>

Cstring Validate(CString str)
{
    for(int i=0; i<str.GetLength(); i++) {
        if(!std::isdigit(str[i]))
            return _T("string contains non numeric characters");
    }
    return _T("string only numeric characters");
}

isdigit使用せずにCString メンバー関数のみを使用する別のソリューションSpanIncluding:

Cstring Validate(CString str)
{
    if(str.SpanIncluding("0123456789") == str)
        return _T("string only numeric characters");
    else
        return _T("string contains non numeric characters");
}
于 2012-10-08T07:24:22.923 に答える
0

CString::Find を使用できます。

int Find( TCHAR ch ) const;

int Find( LPCTSTR lpszSub ) const;

int Find( TCHAR ch, int nStart ) const;

int Find( LPCTSTR pstr, int nStart ) const;

CString str("The stars are aligned");
int n = str.Find('e')
if(n==-1)   //not found
else        //found

MSDNでここを参照してください

于 2012-10-08T10:32:07.500 に答える