1
A
回答
3
也许是这样的:
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());
其更好,以限定可重复使用的功能为擦除remove惯用法
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());
-1
地带的一切,大于127,或见http://www.asciitable.com/,并创建一个更具体的范围
while (CrtChar)
{
if (*CrtChar<35 || *CrtChar>127)
*CrtChar = ' ';
}
0
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);
}
}
}
相关问题
- 1. 如何从C#中的字符串中去除非XHTML标签?
- 2. 从字符串中去除非数字字符
- 3. 如何从SQL Server中的字符串中去除所有非字母字符?
- 4. 如何从字符串中去除非字母数字字符(包括空格)?
- 5. C中的非ASCII字符
- 6. 如何从php字符串中删除ascii字符x02?
- 7. 从CSV中删除非ASCII字符
- 8. 删除字符串中的所有非ASCII字符
- 9. 迭代过去的字符串和从字符串中去除字符串
- 10. 如何从字符串中删除非字母字符?
- 11. 如何从C++中的字符串中去除所有非字母数字字符?
- 12. 除去大部分从字符串非字母字符的elisp
- 13. C++中的非ASCII字符串字符索引
- 14. 如何从JS中的字符串去除不好的字符?
- 15. 如何从字符串中除去一组字符?
- 16. 如何从字符串中去除特定字符和单词
- 17. 如何检测字符串中的非ASCII字符?
- 18. 如何从字符串中删除数字符在C#中
- 19. 如何去除从字符串中的不想要的字符在Objective-C
- 20. 如何用python中的非ascii字符替换字符串中的字符?
- 21. 反转包含ASCII字符和非ASCII字符的字符串
- 22. 从c字符串中删除字符
- 23. 从字符串中删除字符C
- 24. C从字符串中删除字符
- 25. C++从字符串中删除字符
- 26. 从C字符串中删除字符
- 27. 如何从c中的字符串中去除html代码#
- 28. 去除串字符 - Visual C
- 29. 从NSString中删除非ASCII字符在objective-c
- 30. 查找非ASCII字符的字符串
顺便说一句,在真实世界中的应用,通过剥离Unicode字符“解决”在你的代码中的错误是一个坏主意。 –
@Delan Azabani这只发生在文件的末尾。我无法在文件末尾看到该字符(手动)。 – Thangaraj
你可能有一个错误。你确定你的字符串是空的终止? – cprogrammer