谁能告诉我一种有效的方法来验证 CString 对象中存在的数字是有效整数还是 float ?
最佳答案
bool IsValidInt(const CString& text, long& value)
{
LPCTSTR ptr = (LPCTSTR) text;
LPTSTR endptr;
value = _tcstol(ptr, &endptr, 10);
return (*ptr && endptr - ptr == text.GetLength());
}
bool IsValidFloat(const CString& text, double& value)
{
LPCTSTR ptr = (LPCTSTR) text;
LPTSTR endptr;
value = _tcstod(ptr, &endptr);
return (*ptr && endptr - ptr == text.GetLength());
}
编辑:修改代码以遵循评论中提供的优秀建议。
关于c++ - 如何在 VC++ CString 中验证有效的整数和 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4030337/