C++ Strip non-ASCII Characters from string

后端 未结 3 1001
小蘑菇
小蘑菇 2020-12-16 03:52

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         


        
相关标签:
3条回答
  • 2020-12-16 04:21

    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.

    0 讨论(0)
  • 2020-12-16 04:38

    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

    0 讨论(0)
  • 2020-12-16 04:40

    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 toisprint` is undefined behavior.

    0 讨论(0)
提交回复
热议问题