By Roman Okolovich
General The QString class provides a Unicode character string. QString class is used for Initialize a Qstring from  const char * Keep strings Compare strings Manipulate string data (append, prepend, insert, replace, mid, left, right, etc) Construct a complex string from multiple substrings Etc
Important Many of QString's member functions are overloaded to accept  const char *  instead of QString. QString converts the  const char*  data into Unicode using the fromAscii() function QString will allocate memory for the string (malloc function is called) creates a deep copy of the C-style string Applications that define  QT_NO_CAST_FROM_ASCII  don't have access to QString‘s  const char*  API.
Example if (str1.contains( "title" ))  // 1 { QString str2 = str1. mid (str1.indexOf( "TX" )   + 2, 5);  // 2* if (str2  ==   "text1"   ||  // 3 0   ==  str2.compare( "text2" ))  // 4 { // todo } } // hidden malloc is called 4 times // * mid() creates new instance of QString (+1 malloc) bool contains(const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const bool contains(const QRegExp & rx) const bool contains(QRegExp & rx) const int compare(const QString & other) const int compare(const QString & other, Qt::CaseSensitivity cs) const int compare(const QLatin1String & other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const int compare(const QStringRef & ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const bool operator== (const char * other) const The  other  const char pointer is converted to a QString using the fromAscii() function.
Solution if (str1.contains(QLatin1String( "title" ))) { QStringRef str2 = str1.midRef(str1.indexOf(QLatin1String( "TX" ))   + 2, 4); if (str2  ==  QLatin1String( "text1" )   ||  // QString::operator==(const QLatin1String &) 0   ==  str2.compare(QLatin1String( "text2" ))) { // todo } } The QLatin1String class provides a thin wrapper around an ASCII/Latin-1 encoded string literal (It just holds a pointer to the C-string provided in it’s constructor) Thanks to the  QString(const QLatin1String &)  constructor, QLatin1String can be used everywhere a QString is expected. For example: QLabel *label = new QLabel(QLatin1String("MOD"), this); QStringRef is designed to improve the performance of substring handling when manipulating substrings obtained from existing QString instances. QStringRef avoids the memory allocation and reference counting overhead of a standard QString by simply referencing a part of the original string. Calling toString() returns a copy of the data as a real QString instance.
More Efficient String Construction if (foo.startsWith(  &quot;(&quot;  + type +  &quot;) 0x“  ) ) This code requires at least 2 mallocs if (foo.startsWith(QLatin1String( &quot;(&quot; )    + type + QLatin1String (&quot;) 0x&quot; ))) In 4.6, an internal template class QStringBuilder has been added. This class is marked internal and does not appear in the documentation. QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called  once  to get the required space, and the substrings are copied into it one by one. #include  <QStringBuilder> QString hello( &quot;hello&quot; ); QStringRef el(&hello, 2, 3); QLatin1String world( &quot;world&quot; ); QString message = hello % el % world % QChar( '!' );
References QString Class Reference QLatin1String Class Reference Qt wiki

Using QString effectively

  • 1.
  • 2.
    General The QStringclass provides a Unicode character string. QString class is used for Initialize a Qstring from const char * Keep strings Compare strings Manipulate string data (append, prepend, insert, replace, mid, left, right, etc) Construct a complex string from multiple substrings Etc
  • 3.
    Important Many of QString'smember functions are overloaded to accept const char * instead of QString. QString converts the  const char*  data into Unicode using the fromAscii() function QString will allocate memory for the string (malloc function is called) creates a deep copy of the C-style string Applications that define  QT_NO_CAST_FROM_ASCII  don't have access to QString‘s const char* API.
  • 4.
    Example if (str1.contains(&quot;title&quot; )) // 1 { QString str2 = str1. mid (str1.indexOf( &quot;TX&quot; ) + 2, 5); // 2* if (str2 == &quot;text1&quot; || // 3 0 == str2.compare( &quot;text2&quot; )) // 4 { // todo } } // hidden malloc is called 4 times // * mid() creates new instance of QString (+1 malloc) bool contains(const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const bool contains(const QRegExp & rx) const bool contains(QRegExp & rx) const int compare(const QString & other) const int compare(const QString & other, Qt::CaseSensitivity cs) const int compare(const QLatin1String & other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const int compare(const QStringRef & ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const bool operator== (const char * other) const The  other  const char pointer is converted to a QString using the fromAscii() function.
  • 5.
    Solution if (str1.contains(QLatin1String(&quot;title&quot; ))) { QStringRef str2 = str1.midRef(str1.indexOf(QLatin1String( &quot;TX&quot; )) + 2, 4); if (str2 == QLatin1String( &quot;text1&quot; ) || // QString::operator==(const QLatin1String &) 0 == str2.compare(QLatin1String( &quot;text2&quot; ))) { // todo } } The QLatin1String class provides a thin wrapper around an ASCII/Latin-1 encoded string literal (It just holds a pointer to the C-string provided in it’s constructor) Thanks to the  QString(const QLatin1String &) constructor, QLatin1String can be used everywhere a QString is expected. For example: QLabel *label = new QLabel(QLatin1String(&quot;MOD&quot;), this); QStringRef is designed to improve the performance of substring handling when manipulating substrings obtained from existing QString instances. QStringRef avoids the memory allocation and reference counting overhead of a standard QString by simply referencing a part of the original string. Calling toString() returns a copy of the data as a real QString instance.
  • 6.
    More Efficient StringConstruction if (foo.startsWith( &quot;(&quot;  + type +  &quot;) 0x“ ) ) This code requires at least 2 mallocs if (foo.startsWith(QLatin1String( &quot;(&quot; )  + type + QLatin1String (&quot;) 0x&quot; ))) In 4.6, an internal template class QStringBuilder has been added. This class is marked internal and does not appear in the documentation. QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called  once  to get the required space, and the substrings are copied into it one by one. #include <QStringBuilder> QString hello( &quot;hello&quot; ); QStringRef el(&hello, 2, 3); QLatin1String world( &quot;world&quot; ); QString message = hello % el % world % QChar( '!' );
  • 7.
    References QString ClassReference QLatin1String Class Reference Qt wiki