Qt只QStringList的簡單使用方法

2021-08-08 21:53:33 字數 1423 閱讀 4641

qstringlist類提供了乙個字串列表

從qlist 繼承而來,它提供快速索引為基礎的接入以及快速插入和清除

其成員函式用於操作這個字串列表如

1.   增加字串

qstringlist fonts;

fonts << "arial" << "helvetica" << "times" << "courier";

// fonts:[ "arial" ,"helvetica", "times" , "courier"]

2.    合併字串使用join( )

qstring str = fonts.join(",");

// str == "arial,helvetica,times,courier"

3.   拆分字串

qstring str = " arial,helvetica, ,times,courier ";

qstringlist list1 = str.split(",");

// list1: [ " arial ", " helvetica ",」 「, " times ", " courier " ]

qstringlist list2 = str.split(",", qstring::skipemptyparts);

// list2: [ " arial ", " helvetica ", " times ", " courier " ]

也就是說如果有qstring::skipemptyparts,空項不會出現在結果。預設情況下,空項被保留

4.   索引

indexof()函式返回給定字串的第乙個出現的索引。

而lastindexof()函式,返回字串的最後一次出現的索引。

5.    替換replaceinstrings()

qstringlist files;

files << "$qtdir/src/moc/moc.y"

<< "$qtdir/src/moc/moc.l"

<< "$qtdir/include/qconfig.h";

files.replaceinstrings("$qtdir", "/usr/lib/qt");

// files: [ "/usr/lib/qt/src/moc/moc.y", ...]

6.     過濾filter()

可以讓你提取乙個新的列表只包含這些字串包含乙個特定的字串(或匹配特定正規表示式):

qstringlist list;

list << "bill murray" << "john doe" << "bill clinton";

qstringlist result;

result = list.filter("bill");

// result: ["bill murray", "bill clinton"]

QT 之QStringList類常用方法

qstringlist fonts fonts arial helvetica times courier fonts arial helvetica times courier 2.合併字串使用join qstring str fonts.join str arial,helvetica,time...

QList和QStringList的用法

h ifndef mylistview h define mylistview h include class qlistview class qstandarditemmodel class mylistview public qwidget endif mylistview h cpp incl...

QStringList的簡單使用方法

在前段時間研究qt原始碼時,遇到 qstringlist類,下面結合例項,對其用法了解一下。interpolationmethodlabel new qlabel tr interpolation method this interpolationmethodinfo new qcombobox t...