c++character-encodingcross-platformmultibyte

How do I compare single multibyte character constants cross-platform?


As I am writing a cross-platform application, I want my code to work on both platforms.

In Windows, I use this code to compare 2 single characters.

for( int i = 0;  i < str.size();  i++ )
    if( str.substr( i, 1 ) == std::string( "¶" ) )
        printf( "Found!\n" );

Now, in Linux, the character is not found. It is found when I change the substring to a length of 2.

for( int i = 0;  i < str.size();  i++ )
    if( str.substr( i, 2 ) == std::string( "¶" ) )
        printf( "Found!\n" );

How do convert this character comparison code to be cross platform?


Solution

  • Solved using str.compare() and size() of character string:

    std::string str = "Some string ¶ some text ¶ to see";
    std::string char_to_compare = "¶";
    
    for( int i = 0;  i < str.size();  i++ )
        if( str.compare( i, char_to_compare.size(), char_to_compare ) == 0 )
            printf( "Found!\n" );