Before you get started; yes I know this is a duplicate question and yes I have looked at the posted solutions. My problem is I could not get them to work.
b
isprint
depends on the locale, so the character in question must be printable in the current locale.
If you want strictly ASCII, check the range for [0..127]. If you want printable ASCII, check the range and isprint
.
Solution:
bool invalidChar (char c)
{
return !(c>=0 && c <128);
}
void stripUnicode(string & str)
{
str.erase(remove_if(str.begin(),str.end(), invalidChar), str.end());
}
EDIT:
For future reference: try using the __isascii, iswascii commands
At least one problem is in your invalidChar
function. It should be:
return !isprint( static_cast<unsigned char>( c ) );
Casting a char
to an unsigned
is likely to give some very, very big
values if the char
is negative (UNIT_MAX+1 + c). Passing such a
value to
isprint` is undefined behavior.