QT之QStringlist的常见用法

成员函数

1)int size() const
返回列表中元素的数量。

2)void append(const QString &str)
在列表末尾添加一个字符串。

3)void prepend(const QString &str)
在列表开头添加一个字符串。

4)void insert(int index, const QString &str)
在指定位置插入一个字符串。

5)void removeAt(int index)
移除指定位置的字符串。

6)void clear()
清空列表。

7)QString at(int index) const
返回指定位置的字符串。

8)QString first() const
返回第一个字符串。

9)QString last() const
返回最后一个字符串。

10)QString value(int index) const
返回指定位置的字符串,与 at() 函数功能相同。

11)int indexOf(const QString &str, int from = 0) const
返回字符串在列表中的索引位置,从指定位置开始搜索。

12)int indexOf(const QStringRef &str, int from = 0) const
返回字符串在列表中的索引位置,从指定位置开始搜索,使用 QStringRef 类型进行匹配。

13)bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
判断列表是否包含指定字符串。

14)bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
判断列表是否包含指定字符串,使用 QStringRef 类型进行匹配。

15)QStringList filtered(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive, int mode = Qt::MatchContains) const
返回包含指定字符串的过滤后的列表。

16)QStringList filtered(const QRegExp &regexp, int mode = Qt::MatchContains) const
返回匹配正则表达式的过滤后的列表。

17)QString join(const QString &sep) const
使用指定的分隔符将列表中的字符串连接起来并返回连接后的字符串。

18)QString join(const QStringRef &sep) const
使用指定的分隔符将列表中的字符串连接起来并返回连接后的字符串,使用 QStringRef 类型进行匹配。

19)QString join(const QChar &sep) const
使用指定的字符将列表中的字符串连接起来并返回连接后的字符串。

20)QString join(QLatin1String sep) const
使用指定的字符将列表中的字符串连接起来并返回连接后的字符串,使用 QLatin1String 类型进行匹配。

21)QByteArray join(const QByteArray &sep) const
使用指定的分隔符将列表中的字符串连接起来并返回连接后的字节数组。

22)QList toList() const
将列表转换为字节数组列表并返回。

23)QSet toSet() const
将列表转换为字符串集合并返回。

24)bool isEmpty() const
判断列表是否为空。

常见用法

1)创建 QStringList 对象:

QStringList list;  // 创建一个空的字符串列表  
QStringList list1("Hello");  // 创建一个包含单个字符串的字符串列表  
QStringList list2("Hello", "World");  // 创建一个包含两个字符串的字符串列表  
QStringList list3 = QStringList() << "Hello" << "World";  // 使用 << 运算符创建字符串列表

2)添加和删除元素:

list.append("Hello");  // 在列表末尾添加字符串  
list.prepend("World");  // 在列表开头添加字符串  
list.insert(1, "Qt");  // 在指定位置插入字符串  
list.removeAt(1);  // 移除指定位置的字符串  
list.clear();  // 清空列表

3)访问元素:

QString str = list.at(0);  // 返回指定位置的字符串,如果越界会抛出异常  
QString first = list.first();  // 返回第一个字符串,如果列表为空则返回空字符串  
QString last = list.last();  // 返回最后一个字符串,如果列表为空则返回空字符串

4)查找和过滤:

int index = list.indexOf("Hello");  // 返回字符串在列表中的索引位置,如果未找到则返回 -1  
bool contains = list.contains("Hello");  // 判断列表是否包含指定字符串  
QStringList filtered = list.filtered("Hello");  // 返回包含指定字符串的过滤后的列表

5)连接和拆分:

QString joined = list.join(", ");  // 使用指定的分隔符将列表中的字符串连接起来并返回连接后的字符串  
QStringList split = joined.split(", ");  // 使用指定的分隔符将字符串拆分成字符串列表
转换:

QList<QByteArray> byteList = list.toList();  // 将列表转换为字节数组列表并返回  
QSet<QString> stringSet = list.toSet();  // 将列表转换为字符串集合并返回
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

付宇利

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值