【问题标题】:Equality of unicode charsunicode 字符的相等性
【发布时间】:2018-11-06 10:21:52
【问题描述】:

我需要写一个简单的比较。

if (user entered "Д" first)
{
   //do something
}

问题是我需要比较 Unicode 字符(在本例中是俄语字母“Д”)。

我设法通过以下方式做到这一点:

std::string option;
getline(std::cin, option);
if (option.compare(0, 1, u8"Д"))
{
   //do something
}

如何在不使用 std::stringcompare 的情况下使用 char 执行此操作?虽然如果您为std::string 提供更好的解决方案,我会很高兴。

【问题讨论】:

    标签: c++ string unicode


    【解决方案1】:

    远非理想,但它简单且有效(我希望):

    #include <iostream>
    #include <string>
    
    char const yes[] = u8"Д";
    char const no[] = u8"Н";
    
    int main()
    {
        std::string str;
        while (std::cin >> str)
        {
    
            std::cout << "Let's Да? " << str << "! => " << std::boolalpha << (str.substr(0, sizeof(yes) - 1) == yes) << std::endl;
            std::cout << "Let's Нет? " << str << "! => " << std::boolalpha << (str.substr(0, sizeof(no) - 1) == no) << std::endl;
        }
    }
    

    演示:https://ideone.com/Vhtl1T

    Let's Да? Да! => true
    Let's Нет? Да! => false
    Let's Да? Нет! => false
    Let's Нет? Нет! => true
    Let's Да? Нет! => false
    Let's Нет? Нет! => true
    Let's Да? Да! => true
    Let's Нет? Да! => false
    

    【讨论】:

    • 感谢您的回答。这个答案和 Barmak Shemirani 的评论对我帮助很大。
    猜你喜欢
    • 2019-04-10
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    相关资源
    最近更新 更多