how can I compare utf8 string such as persian words in c++?

后端 未结 3 693
抹茶落季
抹茶落季 2020-12-11 10:13

I want to compare strings in Persian (utf8). I know I must use some thing like L\"گل\" and it must be saved in wchar_t * or wstring. the question is when I compare by the fu

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 11:07

    If the strings that you want to compare are in a specific, definite encoding already, then don't use wchar_t and don't use L"" literals -- those are not for Unicode, but for implementation-defined, opaque encodings only.

    If your strings are in UTF-8, use a string of chars. If you want to convert them to raw Unicode codepoints (UCS-4/UTF-32), or if you already have them in that form, store them in a string of uint32_ts, or char32_ts if you have a modern compiler.

    If you have C++11, your literal can be char str8[] = u8"گل"; or char32_t str32[] = U"گل";. See this topic for some more on this.

    If you want to interact with command line arguments or the environment, use iconv() to convert from WCHAR to UTF-32 or UTF-8.

提交回复
热议问题