QStringList介绍
QStringList 提供了非常全面的接口来便捷地实现对字符串集合进行新增、修改、删除、查找等一系列操作。开发者可以根据自身需求,选用适合的方法来操作字符串列表,提升代码效率。
在Qt中,QStringList是一个非常实用的类。它是一个能够方便地管理字符串列表的类,相比于普通的字符串数组,它有以下独特的作用:
字符串列表可动态增长。QStringList可以根据需要动态增长或缩小字符串列表的大小,而无需手动重新分配内存空间。这使得我们可以轻松地向列表中添加或删除元素,避免了因为数据规模不足或超过原先容量所带来的性能和空间问题。
支持许多字符串操作。QStringList提供了丰富的成员函数和操作符,可以很方便地对列表进行字符串插入、删除、查找、排序、去重等操作,并且它支持一些通用的遍历方式,比如迭代器和foreach循环。
易于与其他数据类型转换。QStringList具有良好的互操作性,可以与其他Qt数据类型(例如QVariant、QByteArray甚至QDataStream)直接进行转化,使得数据的处理更加灵活和便捷。
在Qt框架中被广泛应用。由于其方便易用的特性,在Qt框架中QStringList被大量使用于各种场景下,比如在GUI开发中的列表、ComboBox、TreeView等控件,或者在网络传输中的数据序列化和解析等。
下面是QStringList可以使用的所有接口及其作用:
- clear():清空列表中所有元素。
- append(const QString &value)、prepend(const QString &value)、insert(int i, const QString &value):在末尾、开头或者指定位置插入一个元素。
- replace(int i, const QString &value):将指定索引位置的元素替换成新值。
- removeAt(int i)、removeOne(const QString &value)、removeAll(const QString &value):从列表中移除指定位置、匹配的第一个或所有元素。
- count() const、size() const、length() const:获取当前列表的元素个数。
- isEmpty() const:判断列表是否为空。
- operator[](int i)、at(int i) const:通过索引值访问当前列表中的元素。
- indexOf(const QString &value, int from = 0) const、lastIndexOf(const QString &value, int from = -1) const:查找指定元素在列表中第一次或最后一次出现的索引位置。
- startsWith(const QString &value) const、endsWith(const QString &value) const、contains(const QString &value) const:判断列表中是否包含指定前缀、后缀或者任意匹配元素。
- mid(int position, int length = -1) const、left(int count) const、right(int count) const:截取列表中特定位置开始或结束的一段子列表。
- join(const QString &separator) const:将所有元素以指定分隔符连接为单个字符串。
- sorted() const、qSort()、sort(Qt::CaseSensitivity cs = Qt::CaseSensitive):按照字典序排序当前列表,分别返回一个新列表或就地排序。
QStringList使用
#include <QCoreApplication>
#include <QStringList>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//打印日志
auto print = [](const QString &str,const QStringList &list)
{
qDebug()<<str<<":"<<list;
};
// 创建字符串列表
QStringList list1 = {"apple","banana","orange"};
print("origin data",list1);
// 插入新元素到列表末尾和指定位置
list1.append("grape");
print("after append grape",list1);
list1.insert(0,"watermelon");
print("after insert watermelon",list1);
// 访问列表元素并修改
QString fruit = list1.at(2);
fruit += "s";
list1.replace(2,fruit);
print("after replace",list1);
// 从列表中删除元素
list1.removeOne("apple");
print("after removeOne",list1);
list1.removeAll("bananas");
print("after removeAll",list1);
// 判断列表是否为空
if (list1.isEmpty())
qDebug() << "The list is empty.";
// 获取子列表
QStringList sublist = list1.mid(1,2);
print("sublist",sublist);
// 迭代器遍历
QStringList::const_iterator it;
for (it = sublist.constBegin(); it != sublist.constEnd(); ++it)
qDebug() << *it;
// 使用join方法将所有元素以逗号连接成一个字符串
QString fruits_str = list1.join(",");
qDebug() << fruits_str;
// 排序列表
list1.sort();
print("after sort",list1);
//移除重复元素
list1.append("grape");
list1.append("grape");
list1.append("orange");
print("before removeDuplicates",list1);
list1.removeDuplicates();
print("after removeDuplicates",list1);
return a.exec();
}
代码运行输出结果:
"origin data" : ("apple", "banana", "orange")
"after append grape" : ("apple", "banana", "orange", "grape")
"after insert watermelon" : ("watermelon", "apple", "banana", "orange", "grape")
"after replace" : ("watermelon", "apple", "bananas", "orange", "grape")
"after removeOne" : ("watermelon", "bananas", "orange", "grape")
"after removeAll" : ("watermelon", "orange", "grape")
"sublist" : ("orange", "grape")
"orange"
"grape"
"watermelon,orange,grape"
"after sort" : ("grape", "orange", "watermelon")
"before removeDuplicates" : ("grape", "orange", "watermelon", "grape", "grape", "orange")
"after removeDuplicates" : ("grape", "orange", "watermelon")
filter用法
QStringList的filter()函数是一个非常实用的函数,可以帮助我们方便地根据正则表达式过滤出符合条件的字符串元素。QStringList的filter()函数可以根据指定的正则表达式过滤出符合条件的所有元素,返回值也是一个QStringList类型的对象。它的语法如下:
QStringList QStringList::filter(const QRegExp & regExp) const;
其中,regExp参数表示要使用的正则表达式。
下面我们来看一个例子来演示如何使用filter()函数进行字符串列表过滤。假设我们有一个QStringList对象,其中保存了一些文件名:
QStringList files = {"example.txt", "test.doc", "sample.pdf", "demo.jpg"};
QRegExp exp("[a-z]*\\.txt$");
QStringList txt_files = files.filter(exp);
print("txt_files",txt_files);
其中,我们创建了一个QStringList对象files,并初始化了一些包含不同类型文件的文件名。然后,我们创建了一个QRegExp对象exp,它的模式是匹配所有以小写字母开头且以“.txt”结尾的字符串。接下来,我们使用QStringList的filter()函数和exp对象过滤出符合条件的文件名,并将其保存到一个新的QStringList对象txt_files中。最后,我们通过qDebug输出符合条件的文件名。
需要注意的是,在使用过滤器时,正则表达式必须是有效的并符合语法规则。如果正则表达式不正确,则会导致程序出错或无法得到正确的过滤结果。
代码运行输出:
"txt_files" : ("example.txt")