Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] Why there is no toTitleCase for QString



  • Hi Everyone!
    I would like to reformat the case of some text which I recieve at an undetermined format. For Example:

    • illUm QUI dOLOrEm eum ASPeriores REPELLAT

    I have searched for the commonly know function toTitleCase in the Qt database and "found QChar":http://doc.qt.nokia.com/4.7-snapshot/qchar.html#toTitleCase to have this function. In the description of the function:
    [quote]QChar QChar::toTitleCase () const
    Returns the title case equivalent if the character is lowercase or uppercase; otherwise returns the character itself. [/quote]

    • I don't quite understand what this does, or how it should be used.
      ** if you look at a single character alone how will it know if it needs to be titleCased or not?
    • Is there a function under another name which does the same ?
      ** I'm Looking For A Basic Caps Letter At The Begining Of Each Word In The String.

    I was thinking of making a simple algorithm myself to get this done, but if the function already exists then I don't need to reinvent the wheel. :)

    Proposed Algorithm:
    @
    QString str = "illUm QUI dOLOrEm eum ASPeriores REPELLAT";
    QRegExp re("\W\w");
    int pos=-1;
    str = str.toLower();
    QChar * base = str.data();
    QChar * ch;
    do{
    pos++;
    ch = base + pos;
    pos = str.indexOf(re, pos);
    *ch = ch->toUpper();
    }while(pos >= 0);
    @

    Thanks for any constructive criticism / comments / suggestions / contributions, and a great weekend to everyone!

    Edit: Code Error, and better Regular Expression added


  • Moderators

    This is part of what the "unicode standard":http://www.unicode.org/versions/Unicode6.0.0/ch05.pdf#G21180 has to say about titlecase:

    bq. digraph compatibility characters, such as U+01F3 “dz” latin small letter dz, require one form when being uppercased, U+01F1 “DZ” latin capital letter dz, and another form when being titlecased, U+01F2 “Dz” latin capital letter d with small letter z. The latter form is informally referred to as a titlecase character, because it is mixed case, with the first letter uppercase.



  • Aha!
    Now I understand what TitleCase refers to, thanks for that reference.

    Having read a bit now on how complex an actual proper title casing function would be I see why QString does not incorporate it.

    A solution, for someone who would need proper title casing (not my case), would be adding a style and grammar check library.



  • [quote author="Pau" date="1301255549"]Aha!
    Now I understand what TitleCase refers to, thanks for that reference.

    Having read a bit now on how complex an actual proper title casing function would be I see why QString does not incorporate it.

    A solution, for someone who would need proper title casing (not my case), would be adding a style and grammar check library.
    [/quote]

    And of course you would have to adapt it to the localization and internationalization settings to respect the different title casing in different languages.


Log in to reply