September 24, 2010

Sohoquetaros Sohoquetaros
Lab Rat
6 posts

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

  1. for (i = 0; i < message.length(); i++) {
  2. if (message[i] == 'ä')
  3.    // 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

3 replies

September 24, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

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

September 25, 2010

Sohoquetaros Sohoquetaros
Lab Rat
6 posts

Many thanks, this solves my problem !

September 27, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3135 posts

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 [doc.qt.nokia.com]) 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.

 
  ‹‹ QT, MySQL and BLOB      QAction Group Vs. Vector and Array ››

You must log in to post a reply. Not a member yet? Register here!