I have a CString or wchar_t, i would like to be able to check if it is numeric. I cant seem to find the method to do this, any one know it I thought it was IsNumeric() or something like that.
thanks for your help. The isdigit method works. I have a string something like 1234, i wanted to check that each part of the string was numeric, as i will breaking it up for various reasons. so..
CString s = _T("1234"); for (int a = 0; a<s.GetLength;a++) { if (isdigit(s.GetAt(a)) { //code }//if }//for
I think there could be two approaches: 1. IsCharAlphaNumeric && !IsCharAlpha These are Win32 functions, expect TCHAR arguments.
2. isdigit() CRT function This is a CRT function. I don't know if you do not want to link with CRT.
const ULONG g_ccbMaxLen = 0x2000000; // arbitrarily, do not check strings longer than 32MB
bool IsDigit(TCHAR c) { #ifdef I_DONT_WANT_CRT return (IsAlphaNumeric(c) && !IsNumeric(c)); #else // WARNNOTE (ismailp): I am not sure, if this function works with UNICODE chars return isdigit(c); #endif }
/* Return values:
<0 - on failure >=0, number of digits */ int CountDigits(LPCTSTR lpszString) { if (!lpszString) return -1; LPCTSTR lpszSeek = lpszString; if (lstrlen(lpszString) > g_ccbMaxLen) return -1; int iPos = 0, iCount = 0; for (iPos = 0; *lpszSeek && iPos < g_ccbMaxLen; ipos++, lpszSeek++) { if (IsDigit(*lpszSeek)) ++iCount; } return iCount; }
bool IsNumeric(LPCTSTR lpszString) { if (!lpszString) return false; int iLen = lstrlen(lpszString); if (iLen == CountDigits(lpszString)) return true; return false; }
IsNumeric Check
Udhaya Kumar D
Hey,
thanks for your help. The isdigit method works. I have a string something like 1234, i wanted to check that each part of the string was numeric, as i will breaking it up for various reasons. so..
CString s = _T("1234");
for (int a = 0; a<s.GetLength;a++)
{
if (isdigit(s.GetAt(a))
{
//code
}//if
}//for
That works
Thanks for your help
Will
VB 2005 Technical Support
Also, you might want to take a look at http://msdn2.microsoft.com/en-us/library/c707ct0t(en-US,VS.80).aspx if it is helpful.
Thanks,
Ayman Shoukry
VC++ Team
XiNO
1. IsCharAlphaNumeric && !IsCharAlpha
These are Win32 functions, expect TCHAR arguments.
2. isdigit() CRT function
This is a CRT function. I don't know if you do not want to link with CRT.
const ULONG g_ccbMaxLen = 0x2000000; // arbitrarily, do not check strings longer than 32MB
bool IsDigit(TCHAR c)
{
#ifdef I_DONT_WANT_CRT
return (IsAlphaNumeric(c) && !IsNumeric(c));
#else
// WARNNOTE (ismailp): I am not sure, if this function works with UNICODE chars
return isdigit(c);
#endif
}
/*
Return values:
<0 - on failure
>=0, number of digits
*/
int CountDigits(LPCTSTR lpszString)
{
if (!lpszString)
return -1;
LPCTSTR lpszSeek = lpszString;
if (lstrlen(lpszString) > g_ccbMaxLen)
return -1;
int iPos = 0, iCount = 0;
for (iPos = 0; *lpszSeek && iPos < g_ccbMaxLen; ipos++, lpszSeek++)
{
if (IsDigit(*lpszSeek))
++iCount;
}
return iCount;
}
bool IsNumeric(LPCTSTR lpszString)
{
if (!lpszString)
return false;
int iLen = lstrlen(lpszString);
if (iLen == CountDigits(lpszString))
return true;
return false;
}
Should be ok. I have not compiled it.
Ismail