我想清理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的。 –