验证一个CString变量 [英] Validate a CString variable
本文介绍了验证一个CString变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想检查CString变量是否仅包含数字和字符:"/".我使用了FindOneOf函数,但是不起作用.
Hi all,
I want to check if a CString variable contains only numbers and the character: "/". I used the FindOneOf function but that doesn''t work.
CString startDate;
if ( startDate.FindOneOf(_T("abcdefghijklmnopqrstuvwxyz")) == true )
有一个更好的方法吗?日期格式为"dd/mm/yyyy"
Is there a better way to do this? The date format is "dd/mm/yyyy"
推荐答案
您可以编写一个简短的解析器来检查每个字符:
You may write a short parser that checks each character:
LPCTSTR lpszParse = strDate.GetString();
while (*lpszParse && _tcschr(lpszParse, _T("01234567890/")))
lpszParse++;
if (*lpszParse == _T(''\0'') && !strDate.IsEmpty())
{
// valid string
}
但是使用固定的日期格式,您也可以将其完全解析以检查有效日期:
But with a fixed date format you may also parse it completely to check for a valid date:
int nDay = _tstoi(strDate.GetString());
int nMonth = _tstoi(strDate.GetString() + 3);
int nYear = _tstoi(strDate.GetString() + 6);
if (strDate.GetAt(2) == _T('/') &&
strDate.GetAt(5) == _T('/') &&
// strDate.GetLength() == 10 &&
nDay >= 1 && nDay <= 31 &&
nMonth >= 1 && nMOnth <= 12)
{
// valid
}
您使用的是MFC吗?
这可能有效:
Are you using MFC?
This might work:
CString startDate = "01/12/2012";
COleDateTime date;
if (date.ParseDateTime (startDate, VAR_DATEVALUEONLY))
{
// Parse succeeded, so it's a valid date
}
这篇关于验证一个CString变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文