Home | Unicode Tasks | Multibyte Character Set (MBCS) Tasks
Use the following tips:
if ( sz[ strlen( sz ) - 1 ] == '\\' ) // Is last character a '\'?
// . . .
Since strlen is not “MBCS-aware,” it will return the number of bytes, not the number of characters, in a multibyte string. Also, note that in some code pages (932, for example), ‘\’ (0x5c) is a valid trail byte (sz
is a C string).
One possible solution is to rewrite the code this way:
char *pLast;
pLast = _mbsrchr( sz, '\\' ); // find last occurrence of '\' in sz
if ( pLast && ( *_mbsinc( pLast ) == '\0' ) )
// . . .
This code uses the MBCS functions _mbsrchr and _mbsinc. Because these functions are MBCS-aware, they can distinguish between a ‘\’ character and a trail byte ‘\’. The code performs some action if the last character in the string is a null (‘\0’).
See Also Character Assignment