【发布时间】:2018-12-15 01:58:15
【问题描述】:
我正在尝试检测一些 Unicode 字符组合(如 ​)来清理字符串,对于单个 Unicode 字符它正在检测但 Unicode 组合没有检测到。
我用来从另一个需要清理的 HTML 页面制作 HTML 页面的字符串。我只想清理具有这种 unicode 的字符串,这些 unicode 甚至在浏览器的 html 页面中都不可见。
下面是示例代码:
void detect_Unicode(string& str) {
if(!str.empty() && str.find_first_not_of(" \t\n\r\f\v\u00A0\u00C2\u00E2\u20AC\u2039")==string::npos)
str.assign(" ");
return;
}
输入字符串:
1. " ​ ​ " ;
2. "are   there is something    ​ combination ​"
3. " Â Â "
4. "​   ​"
5 . "Â Â â â"
预期输出:
1. " "
2. "are   there is something    ​ combination ​"
3. " "
4. " "
5. " "
请告诉我其他方法。
【问题讨论】:
-
如果可以,请使用
std::wstring -
std::string不包含 unicode 字符,而是“编码”字节(可能是 utf-8)。所以对于多字节字符,你必须使用std::search而不是find_first_not_of。 -
@PaulSanders:
wchar不保证为 2,即使在这种情况下,unicode 也可能需要多个wchars。 -
@Jarod42 你能解释一下我如何使用
std::search和string -
@Jarod452 wchar 不保证是 2 我想我从来没有声称它是。
标签: html c++ string unicode character-encoding