How to compare multibyte characters in C

前端 未结 4 1238
感动是毒
感动是毒 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:08

    There are no standards surrounding embedding non-ASCII characters directly in your source file.

    Instead, the C11 standard specifies that you can use Unicode code points:

    wchar_t text[] = L"\u00f6\u00e7\u015f\u0131\u011f";
    
    // Print whole string
    wprintf(L"%s\n", text);
    
    // Test individual characters
    for (size_t i = 0; text[i]; ++i)
    {
        if ( text[i] == u'\u00f6' )
            // whatever...
    }
    

    If you are in Windows then you face an extra problem that the Windows console can't print Unicode characters by default. You need to do the following:

    • Change the console to use a TrueType monospaced font which includes glyphs for the characters you are trying to print. (I used "DejaVu Sans Mono" for this example)
    • In the source code, call the function _setmode(1, _O_WTEXT); , which will need #include .

    To restore normal text afterwards you can _setmode(1, _O_TEXT);.

    Of course, if you are outputting to a file or to a Win32 API function then you don't need to do those steps.

提交回复
热议问题