如何从字符串中去掉非ascii字符?在c++中
How can you strip non-ASCII characters from a string? in c++
如何从字符串中剥离非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 = ' ';
}
相关文章:
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 有没有办法通过使用十进制 ASCII 代码自动类型扣除来获取字符?
- RE2 不匹配非 ASCII 字符
- 打印从 0 到 255 的所有 ASCII 字符
- C++ 电话号码字母拨号程序 - 语法错别字?还是非 ASCII 字符?
- 无符号字符打印其 ASCII 值
- 如何从 QString 中包含的十六进制值中获取 ASCII 字符?
- 在结构数据类型中更改每个字符的 ASCII 值
- Cin 和 getline 无法正确保存非 ascii 字符
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- 非ASCII字符的词典排序
- 将char类型指定给int以打印相应的ASCII字符
- 将非 ASCII 字符转换为英语对应字符C++
- 在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串
- C++ UTF-8 瑞典语字符读取为 ASCII
- 将十六进制字符数组存储在字节数组中,而无需更改为 ASCII 或其他任何东西
- 如何在不将其转换为 ASCII 字符的情况下显示 QByteArray 的十六进制值
- 如何遍历所有 ASCII 字符?
- 读取字符 ASCII 值并将其记录到矢量
- 如何在C++中读取带有ifstream的替换字符?(ASCII 中的 SUB)