• 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 ®exp, 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";  // 使用 << 运算符创建字符串列表
    
    • 1
    • 2
    • 3
    • 4

    2)添加和删除元素:

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

    3)访问元素:

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

    4)查找和过滤:

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

    5)连接和拆分:

    QString joined = list.join(", ");  // 使用指定的分隔符将列表中的字符串连接起来并返回连接后的字符串  
    QStringList split = joined.split(", ");  // 使用指定的分隔符将字符串拆分成字符串列表
    转换:
    
    QList<QByteArray> byteList = list.toList();  // 将列表转换为字节数组列表并返回  
    QSet<QString> stringSet = list.toSet();  // 将列表转换为字符串集合并返回
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    书生·浦语大模型开源体系(七)作业
    【数据结构篇】堆
    分类预测|基于黑翅鸢优化最小二乘支持向量机分类预测Matlab程序BKA-LSSVM 多特征输入多类别输出 含基础LSSVM
    SKG 渠道中台借助 SAE + 大禹打造云原生 DevOPS,提效 60%
    【文献copilot】调用文心一言api对论文逐段总结
    【Redis GEO】1、地理位置类型的基本用法
    目前期货开户手续费比较透明
    每日OJ题_其它背包问题④_力扣96. 不同的二叉搜索树(卡特兰数)
    百分号%的输出:
    微服务实战 06 分布式事务框架 seata AT 模式 和 Saga 模式
  • 原文地址:https://blog.csdn.net/techenliu/article/details/133144004