如何从字符串中去掉非ascii字符?在c++中

How can you strip non-ASCII characters from a string? in c++

本文关键字:字符 ascii c++ 字符串      更新时间:2023-10-16

如何从字符串中剥离非ascii字符?

我想知道我们如何在c++中实现这个

可能是这样的:

struct InvalidChar
{
    bool operator()(char c) const {
        return !isprint(static_cast<unsigned char>(c));
    }
};
std::string s;
HypoteticalReadFileToString(&s);
s.erase(std::remove_if(s.begin(),s.end(),InvalidChar()), s.end());

最好为擦除-删除习惯用法定义一个可重用的函数

template <typename C, typename P> 
void erase_remove_if(C& c, P predicate) {
    c.erase(std::remove_if(c.begin(), c.end(), predicate), c.end());
}

erase_remove_if(s, InvalidChar());

void stringPurifier ( std::string& s )
{
    for ( std::string::iterator it = s.begin(), itEnd = s.end(); it!=itEnd; ++it)
    {
      if ( static_cast<unsigned int>(*it) < 32 || static_cast<unsigned int>(*it) > 127 )
      {
        (*it) = ' ';
      }
    }
}
void stringPurifier ( std::string& dest, const std::string& source )
{
  dest.reserve(source.size());
  for ( std::string::const_iterator it = source.begin(), itEnd = source.end(); it!=itEnd; ++it)
  {
    if ( static_cast<unsigned int>(*it) < 32 || static_cast<unsigned int>(*it) > 127 )
    {
      dest.push_back(' ');
    }
    else
    {
      dest.push_back(*it);
    }
  }
}

删除大于127的所有内容,或者查看http://www.asciitable.com/并创建更具体的范围

while (CrtChar)
{
  if (*CrtChar<35 || *CrtChar>127)
   *CrtChar = ' ';
}