Comparing unicode chars



  • Hello together,

    maybe this is a very simple thing, but I didn't find any solution yet:

    I want to compare char by char a QString - like this

    @for (i = 0; i < message.length(); i++) {
    if (message[i] == 'ä')
    // do somthing
    @

    The point is: it doesn't work. "ä" is a 16bit unicode char and I don't know how to to the comparison right.

    Can anybody help me? That would be very nice.

    Best regards



  • Just use QChar with either 16-bit code in constructor or cell and row constructor instead of 'ä'.



  • Many thanks, this solves my problem !


  • Moderators

    Are you aware that 'ä' can be encoded in different ways in unicode? Your simple comparison only detects one way to encode it!

    You might want to normalize (see "QString::normalized":http://doc.qt.nokia.com/4.7/qstring.html#normalized) both strings before comparing them or be prepared to have strings that produce the same sequence of glyphs (the little drawings on the screen) be considered different.

    Some normalization forms encode 'ä' in a sequence of unicode codepoints. In that case your character-by-character comparison will miss it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.