Den Andreychuk
Den Andreychuk

Reputation: 478

Equality of unicode chars

I need to write a simple comparison.

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

The problem is that I need to compare the Unicode characters (in this case, the Russian letter "Д").

I managed to do this in the following way:

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

How can I do this with char, without using std::string with compare? Although I would be happy if you offered a better solution for std::string.

Upvotes: 2

Views: 96

Answers (1)

Killzone Kid
Killzone Kid

Reputation: 6240

Far from ideal, but it simple and works (I hope):

#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;
    }
}

Demo: 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

Upvotes: 1

Related Questions