【发布时间】:2021-06-09 13:16:50
【问题描述】:
我的代码应该清除任何不是a-z 或A-Z 的字符。对于其他字符,例如á à ã ă â é è ê,如果我可以使其工作,我会让它们从á 更改为a 和è 更改为e 等。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int counter=0;
string* word = new string[1];
string b ="áhelloá";//nothing
word[0] = "áapple_.Dogá.";//doesnt work
//word[0] = "apple_.Dog.";//if there is no characters like á it works
cout<<endl<<word[0].length()<<endl;
for (int i = 0; i < word[0].length(); ++i)
{
if(word[0][i] >= 'A' && word[0][i] <='Z' || word[0][i] >= 'a' && word[0][i] <='z')
{
cout<<"Current: "<<word[0][i]<<endl;//shows what characters passed if
}
else
{
cout<<"Erased: "<<word[0][i]<<endl;//shows what was erased
word[0].erase(i,1);//deletes char
i--;
}
}
cout<<endl<<word[0];//prints final word,after erase
return 0;
}
如果我在Clion 中使用例如á 运行我的代码,它不会执行任何操作并返回0。我在Repl.it 上进行了相同的测试,我认为它在某种程度上按预期工作。我的Clion 有问题吗?我做错了什么?
【问题讨论】:
-
char无法表示非 ASCII 字符。您需要 utf-8 或其他编码方案来处理字符。你需要一个不是std::string的不同的。 -
@FredLarson 为什么它适用于 Replit?我查看了一些关于 unicode 的帖子,但它对我来说有点复杂,而且我也没有一些库。
-
您必须使用
std::wstring、std::wcout和L"abcdef"。另外:你是从哪里学习 c++ 的?!为什么你无缘无故地使用动态数组?!还使用命名空间 std 和 endl! -
@Jesepy 还有
std::string在某些操作系统上可能就足够了(例如,它可以在我的 Mac 上运行),但在其他操作系统上就不行了。
标签: c++ string text ascii extended-ascii