How to use QStringList in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

Template:Abstract 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[edit | edit source]

  • 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[edit | edit source]

  1. include <QApplication>
  2. include <QString>
  3. 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[edit | edit source]

Documentation for QStringList
Originally by James1980, updates by Mind Freak