Skip to content

Commit 226b2d5

Browse files
committed
Add String::toHeadlineCase(), capitalize first letter of all words
1 parent 25f5d0c commit 226b2d5

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/core/include/qx/core/qx-string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class QX_CORE_EXPORT String
6666
static QString trimTrailing(const QStringView string);
6767

6868
static QString mapArg(QAnyStringView s, const QMap<QString, QString>& map, Qt::CaseSensitivity cs = Qt::CaseSensitive);
69+
70+
static QString toHeadlineCase(const QString& string);
6971
};
7072

7173
}

lib/core/src/qx-string.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,30 @@ QString String::mapArg(QAnyStringView s, const QMap<QString, QString>& args, Qt:
379379
return result;
380380
}
381381

382+
/* Capitalizes the first letter of every word in @a string, and ensures the rest of the letters
383+
* are in lower case. This is not the same as Title Case, where some words are never capitalized.
384+
*
385+
* This function considers a 'word' to be distinct after any whitespace occurs.
386+
*/
387+
QString String::toHeadlineCase(const QString& string)
388+
{
389+
QString hc(string);
390+
391+
bool firstCh = true;
392+
for(QChar& ch : hc)
393+
{
394+
if(ch.isSpace())
395+
firstCh = true;
396+
else if(firstCh)
397+
{
398+
ch = ch.toUpper();
399+
firstCh = false;
400+
}
401+
else
402+
ch = ch.toLower();
403+
}
404+
405+
return hc;
406+
}
407+
382408
}

0 commit comments

Comments
 (0)