Re: test if a string is a valid 'number'?
Point taken. I didn't know his strings would be that complex. I guess the
only way to do it completely would be to loop through the string. I suppose
you could do something like (I just wrote this out, didn't really test it,
but you get the idea):
bool IsANumber(const CString& cs)
{
LPCTSTR szTest = cs;
bool bFoundDot = *szText == _T('.');
bool bNum = true;
//If we have a + or - it should be in the first position. Also could be
decimal point or a number
if ((*szTest > _T('9') || *szTest < _T('0')) && *szTest != _T('-') &&
*szText != _T('+') && !bFoundDot)
bNum = false;
++szTest;
while (bNum && *szTest) {
// We only want one decimal point
if(!bFoundDot && *szTest != _T('.'))
bFoundDot = true;
else if (*szTest > _T('9') || *szTest < _T('0')) {
bNumeric = false;
break;
}
++szTest;
}
return bNum;
}
Of course you could add a lot ot this for other kinds of tests (like
scientific notation. If you already have reg ex in your code then your way
is likely better.
Tom
"MrAsm" <mrasm@usa.com> wrote in message
news:v5er539sq6meroel6813g04161rms44o7f@4ax.com...
On Wed, 30 May 2007 10:23:12 -0700, "Tom Serface"
<tom.nospam@camaswood.com> wrote:
If you are using a CString you can use the SpanExcluding() function to
check
if all of the characters are numbers, +, -, or period. Something like:
CString result = csNumber.SpanExcluding(_T("0123456789+-."));
Then if result it not empty (result.IsEmpty()) then you know there are
other characters in the list.
Tom
Hi Tom,
but if result is empty, it could be *not* a number, e.g.
csNumber = _T("++332.-232.+2002+-22-1+");
is it correct?
MrAsm