last day (14 days later) » 

12:02 AM
2
Q: Equality of unicode chars

Den AndreychukI 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.com...

 
Your use of compare is wrong. It returns a value that is "true" if the string you're looking for is not found in the first char. First of all you need to compare the result of compare with zero; Secondly the character Д can't be encoded using a single char in UTF-8.
 
Collation and comparison in a unicode aware way can also be provided using the ICU libraries. site.icu-project.org
 
Looks like you have multi-byte narrow string there, means a letter might occupy more than 1 byte. This means that char comparison won’t quite work. You can try wchar_t But this means you have to use wstring
 
I tried to use char16_t, but in that case std :: cin does not work.
probably it is necessary as that to compare const char yes [] = u8 "Д"
 
Does the string only contain that glyph? If so, compare the whole string.
 
12:02 AM
no, the user can enter something else besides the "Д". It is necessary to compare only this symbol.
 
Right, but if you want to check that the input isn't "Д", it might be enough to compare two std::string objects. If you want to check whether a string contains that glyph, then it is more complicated. Part of the std::string interface is basically a byte array.
 
Are you looking for platform independent solution?
 
In my case, I need to check the first character. Although I can not drop the option that the user enters "Да" or "Да, я хочу продолжить работу" In both cases, the program must compare the first letter "Д" and return true.
 
u8"Д" is represented by 2 characters in UTF8. You can't treat it as a single character. You can try if(u8.find(u8"Д") != std::string::npos) {...} See this answer stackoverflow.com/a/50455264/4603670
 
@KillzoneKid No, I'm working on my coursework. One of the requirements, the whole program should be written in Ukrainian. Therefore, I faced such problems for the first time. It will be enough if this will work correctly only on my computer (I'm using macOS, Xcode)
@BarmakShemirani TNX IT'S WORK PERFECT:3
@BarmakShemirani One more question. If Unicode is not represented as 1 character, then tell me how to use the toupper () function to provide "Д" and "д"
 
12:02 AM
Unfortunately there is currently no toupper in UTF8. I think the Mac solution is ICU library mentioned earlier.
 
@BarmakShemirani: Or better, IMO, wrap NSString. ICU is a bit heavyweight for something as simple as this.
@BarmakShemirani: shouldn't that be if(u8.find(u8"Д") == 0?
 
@PaulSanders if the string::find returns 0 it means u8"Д" started at position 0. On the other string::npos indicates "not found"
 
@BarmakShemirani I thought that's what he wanted - to see if the string starts with that character.
 
@PaulSanders you are right. It should test for 0 in this case.
By the way, if you are only interested in Russian alphabet you can just make your own function to handle toupper and tolower. The standard toupper works for ASCII, you have to be careful not to mix them up.
 

last day (14 days later) »