我想清理C++中的字符串。我想清除所有非字母字符,并且保留所有种类的英文和非英文字母。我的一个测试的代码看起来像这样清理非字母字符的字符串ma
int main()
{
string test = "Danish letters: Æ Ø Å !!!!!!??||~";
cout << "Test = " << test << endl;
for(int l = 0;l<test.size();l++)
{
if(!isalpha(test.at(l)) && test.at(l) != ' ')
{
test.replace(l,1," nope");
}
}
cout << "Test = " << test << endl;
return 0;
}
这使我的输出:
Test = Danish letters: Æ Ø Å !!!!!!??||~
Test = Danish letters nope nope nope nope nope nope nope nope nope nope nope nope nope nope nope nope nope nope"
所以我的问题是,我怎么删除“!!!!! !|| ||“而不是”ÆØÅ“?
我也试了测试,如
test.at(l)!='Å'
,但我我不能编译,如果我宣布“A”为char。
我读过关于unicode和utf8的内容,但我不太明白。
请帮我:)
那么,你需要不断阅读关于Unicode和UTF8直到你了解它,然后一切都应该是一清二楚。 –
您可能想看看标题为[如何从字符串中去除所有非字母数字字符]的SO问题(http://stackoverflow.com/questions/6319872/how-to-strip-all-non-alphanumeric-characters-从-A-字符串在-C)。我也有兴趣看看[std :: isalnum](http://en.cppreference.com/w/cpp/string/byte/isalnum)是否适用于你的情况。 – 2016-10-01 20:49:29
@RawN:这两个链接仅适用于ASCII,这个问题(隐含地)是关于非ASCII的。 –