不能使用像 á à ã ă â é è ê 这样的字符

如何解决不能使用像 á à ã ă â é è ê 这样的字符

我的代码应该清除任何不是 a-zA-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有问题吗?我做错了什么?

解决方法

您可以在 Windows 中使用 wcoutwstring 处理 C++ 中的 Unicode 字符:

#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>
using namespace std;

int main()
{
    _setmode(_fileno(stdout),_O_U16TEXT); //set the mode of the output file handle to take only UTF-16 data
    int counter=0;
    wstring word;

    word = L"áapple_.Dogá.";
    cout<<'\n'<<word.length()<<'\n';

    for (int i = 0; i < word.length(); ++i)
    {
        if(word[i] >= 'A' && word[i] <='Z' || word[i] >= 'a' && word[i] <='z')
        {
            wcout<<"Current: "<<word[i]<<'\n';
        }
        else
        {
            wcout<<"Erased: "<<word[i]<<'\n';
            word.erase(i,1);
            i--;
        }
    }

    wcout<<'\n'<<word;
    return 0;
}

结果:

Erased: á
Current: a
Current: p
Current: p
Current: l
Current: e
Erased: _
Erased: .
Current: D
Current: o
Current: g
Erased: á
Erased: .

appleDog

对于“为什么它适用于 repl.it?”这个问题:

应该注意的是,不同的编译器和平台对 Unicode 字符的处理非常不同。引用 @bames53 :

#include <iostream>

int main() {
    std::cout << "Hello,ф or \u0444!\n"; }

这个程序不要求 'ф' 可以用单个表示 字符。在 OS X 和大多数现代 Linux 安装上,这将正常工作 很好,因为源代码、执行代码和控制台编码都将是 UTF-8(支持所有 Unicode 字符)。

Windows 更难,而且有不同的可能性 有不同的权衡。

顺便说一句,IMO 您无缘无故地使用动态数组。一个 wstring 就足够了。

另见

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?