7

I am thinking of regular expressions, but that is not exactly readable. There are also functions like s.toUpper() to consider, and probably other things as well.

So what is the best method for capitalising the first letter of words in a QString?

Anon
  • 2,267
  • 3
  • 34
  • 51
  • 1
    What do you mean by "best"? Most efficient, most readable, most maintainable? There isn't a great deal of information here. Do you have 1000 `QString` that you want to handle, or are you just starting? – Tas Dec 21 '16 at 00:39
  • 3
    I can't see any better method than iterating over the string and capitalizing every letter after a space. – Paul Rooney Dec 21 '16 at 01:21
  • 1
    Yes any sort of loop you like I guess which lets you get at the characters. I don't know a bean about QT but i'd think a range based for loop would be best choice for the sake of brevity. – Paul Rooney Dec 21 '16 at 01:57
  • 2
    See [this example](https://wiki.qt.io/Converting_Strings_from_and_to_Camel_Case). – thuga Dec 21 '16 at 05:57

5 Answers5

9

Using this example as a reference, you can do something like this:

QString toCamelCase(const QString& s)
{
    QStringList parts = s.split(' ', QString::SkipEmptyParts);
    for (int i = 0; i < parts.size(); ++i)
        parts[i].replace(0, 1, parts[i][0].toUpper());

    return parts.join(" ");
}
thuga
  • 12,601
  • 42
  • 52
1

Exactly the same, but written differently :

QString toCamelCase(const QString& s)
{
    QStringList cased;
        foreach (QString word, s.split(" ", QString::SkipEmptyParts))cased << word.at(0).toUpper() + word.mid(1);

    return cased.join(" ");
}

This uses more memory but is without pointer access (no brackets operator).

andrey.s
  • 789
  • 10
  • 28
1

There is an alternative way of doing this which iterates using references to the words and modifies the first character using a QChar reference instead:

QString capitalise_each_word(const QString& sentence)
{
  QStringList words = sentence.split(" ", Qt::SkipEmptyParts);
  for (QString& word : words)
    word.front() = word.front().toUpper();

  return words.join(" ");
}

Note that Qt::SkipEmptyParts is required here (as in the other answers to this question) since the first character of each word is assumed to exist when capitalising. This assumption will not hold with Qt::KeepEmptyParts (the default).

darrenp
  • 4,265
  • 2
  • 26
  • 22
0

If the QString is placed in any item that uses QFont, you can use its setCapitalization property to change text to title case. Here is an example - my code is very happy to have discovered it.

QFont formatFont = m_formatNameEdit->font();
formatFont.setCapitalization(QFont::Capitalize);
m_formatNameEdit->setFont(formatFont);

Thanks to an answer from Qt Centre Thread: How to capitalize a string

Thalia
  • 13,637
  • 22
  • 96
  • 190
-2

Incredible C++/Qt... You just want to get some chars ored with 0x20...

Folco
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 07 '22 at 09:23
  • Yeah, sure, always work to make an ASCII "upcase", when the string can be Unicode, UTF-8, non-latin, etc. – Wisblade Jan 09 '23 at 10:04