How to compare multibyte characters in C

前端 未结 4 1240
感动是毒
感动是毒 2021-01-17 19:11

I try to parse text and find some characters in it. I use the code below. It works with normal characters like abcdef but it does not work with öçşğüı

4条回答
  •  耶瑟儿~
    2021-01-17 20:04

    The best way to handle wide characters is as, well, wide characters.

    wchar_t myWord[] = L"Something";
    

    This will do it:

    #include 
    #include 
    #include 
    
    int main()
    {
        wchar_t * text = L"öçşğü";
        int i = 0;
    
        while (text[i])
        {
            if (text[i] == L'ö')
            {
                wprintf(L"ö \n");
            }
    
            i++;
        }
    
        return 0;
    }
    

    If you're in Visual Studio, like me, recall that the console window doesn't handle Unicode well. You can redirect it to a file and examine the file, and see the ö.

提交回复
热议问题