Alex Rivera | Logout

isalpha equivalent for wchar_t

Asked 2010-07-31T13:43:10.897
12

what is the equivalent function for isalpha or isalnum using wchar_t?

wctype ?

an example would be nice also

thanks

Edit
Report

2 Answers

3

You include tag "localization" in your question. In case of writing of international application you should clear define what do you mean under alphabetical or numerical characters. If you write programs for Windows I'll recommend you to use GetStringTypeEx function (see http://msdn.microsoft.com/en-us/library/dd318118.aspx). For example the code

BOOL bSuccess;
int isTrue;
WORD wCharType;

bSuccess = GetStringTypeExW (LOCALE_USER_DEFAULT, CT_CTYPE1, L"a", 1, &wCharType);
if (wCharType & C1_ALPHA == C1_ALPHA) {
    // 
}

You can also use CT_CTYPE3 or CT_CTYPE2 to determne whether a charachter is an Ideographic or whether it is an European number.

To be more exact just try to use function iswalpha, IsCharAlphaW, iswalnum, iswdigit and GetStringTypeExW to test following charachters: L'a', L'ü', L'á', L'я' (Russian charackter), L'ノ' (Japanese charackter in Katakana), L'一' (1 in Japanese). You will see that

  • iswalpha (L'ノ') return alpha
  • IsCharAlphaW (L'ノ') return NOT alpha
  • iswalnum (L'一') return alpha or digit
  • iswdigit (L'一') return NOT digit

The code

bSuccess = GetStringTypeExW (LOCALE_USER_DEFAULT, CT_CTYPE2, L"一", 1, &wCharType);
if ((wCharType & C2_EUROPENUMBER) == wCharType) {
    // numeric
}

say you that L"一" is NOT a european number. You can use GetStringTypeExW to destinduish european number from for example arabic number etc.

So I recommend you to specify your requirement more exactly and then choose the API based on the requirements. In general the usage of the C API is not the best way for an international application.

answered 2010-08-01T16:49:04.997
1

It depends on how you define “equivalent.” The C character classes are quite simple minded compared to Unicode character classes. For example, if you want to test whether a given code point usually represents a letter (for some definition of “letter”), you could test for the general category L; if you want to check whether a given string comprises a valid identifier, you could use UAX #31, etc. iswalnum and iswalpha might give the intended result depending on the current “locale” setting.

answered 2010-08-01T13:57:29.590

Your Answer