Any API to find if cstring contains special characters

A

Alamelu

Hi,

I want to check if cstring contains alphabets, numerics and comma. No other
characters are allowed. Any methods available in cstring?

The below code doesnt work. Could you Plz suggest any other better approach

CString strTemp = _T("abcde,54");
for(int i = 0; i <= strTemp.GetLength() ; i++)
{
if(isalnum(strTemp)
||strTemp == ",")
{
}
else
{
cout << "Contains illegal char";
}
}

Regards,
Alamelu
 
D

David Lowndes

I want to check if cstring contains alphabets, numerics and comma. No other
characters are allowed. Any methods available in cstring?

The below code doesnt work. Could you Plz suggest any other better approach

CString strTemp = _T("abcde,54");
for(int i = 0; i <= strTemp.GetLength() ; i++)
{
if(isalnum(strTemp)
||strTemp == ",")
{
}
else
{
cout << "Contains illegal char";
}
}


Here's your corrected code:

CString strTemp = _T("abcde,54");
for(int i = 0; i < strTemp.GetLength() ; i++)
{
if(isalnum(strTemp)
||strTemp == ',' )
{
}
else
{
cout << "Contains illegal char";
}
}

Dave
 
M

Mihai N.

I want to check if cstring contains alphabets, numerics and comma. No other
characters are allowed. Any methods available in cstring?

Depends what you mean by alphabets and numerics.

isalnum is locale sensitive.
If you call a setlocale somewhere in your application then it will
be true for any character that is an alphanumeric *for that language*
That will also include accented characters.

That functionality also makes the function a bit slower even if you don't
call setlocale.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top