Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Converting Strings from and to Camel Case: Difference between revisions
Jump to navigation
Jump to search
(Created page with "== Convert from camelCaseString to camel_case_string == <code> QString fromCamelCase(const QString &s) { static QRegularExpression regExp1 {"(.)([A-Z][a-z]+)"}; static...") |
(Sub-categorize) |
||
(3 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Snippets::Misc]] | |||
== Convert from camelCaseString to camel_case_string == | == Convert from camelCaseString to camel_case_string == | ||
<code> | <code> |
Latest revision as of 11:55, 28 November 2016
Convert from camelCaseString to camel_case_string
QString fromCamelCase(const QString &s)
{
static QRegularExpression regExp1 {"(.)([A-Z][a-z]+)"};
static QRegularExpression regExp2 {"([a-z0-9])([A-Z])"};
QString result = s;
result.replace(regExp1, "\\1_\\2");
result.replace(regExp2, "\\1_\\2");
return result.toLower();
}
Convert from camel_case_string to camelCaseString
QString toCamelCase(const QString& s)
{
QStringList parts = s.split('_', QString::SkipEmptyParts);
for (int i=1; i<parts.size(); ++i)
parts[i].replace(0, 1, parts[i][0].toUpper());
return parts.join("");
}