Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to use QStringList in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QStringList
Created: james1980 (17 Jan 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This article demonstrates how to use QStringList. QStringList inherits from QList<QString>. Like QList, QStringListis implicitly shared. It provides fast index-based access as well as fast insertions and removals. Passing string lists as value parameters is both fast and safe.

Various Function

  • To make a String List.
QStringList name,namesplit;
name << "james 1980" << "james bond" << "paul" << "jonny joker";
  • To join all the string in string list.
str1 = name.join(",");//str1="james 1980,james bond,paul,jonny joker"
  • To break up a string into a string list.
namesplit = str1.split(",");// namesplit is same as name
  • Extract a new list which contains only those strings which contain a particular substring (or match a particular regular expression).
namesplit = name.filter("james");//namesplit = ["james 1980","james bond"]
  • To replace String.
namesplit.replaceInStrings("a", "o");

Code Snippet

#include <QApplication>
#include <QString>
#include <QStringList>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString str = "world";
QString str1;
QStringList name,namesplit;
name << "james 1980" << "james bond" << "paul" << "jonny joker";
str1 = name.join(",");//str1="james 1980,james bond,paul,jonny joker"
namesplit = str1.split(",");// namesplit is same as name
namesplit = name.filter("james");//namesplit = ["james1980","james bond"]
bool a = name.contains(str);//Returns true if the list contains the string str; otherwise returns false.
namesplit.replaceInStrings("a", "o");// Replace all the occurrence of "a" with "o"
name.sort();//Sorts the list of strings in ascending order (case sensitively).
return app.exec();
}

See also

Documentation for QStringList
Originally by James1980, updates by Mind Freak

This page was last modified on 11 October 2012, at 01:17.
252 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×