当前位置:网站首页>QStringList 的学习笔记
QStringList 的学习笔记
2022-06-27 21:35:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
因公司项目,开始学习QT,这里做一些学习笔记,一遍以后忘记了可以翻阅。 笔记内容写的简单,勿怪。 参考博客:https://blog.csdn.net/u013360881/article/details/52170487
QStringList 初始化
QStringList qstrList;
qstrList<<"Android" << "Qt Creator" << "Java" << "C++";
QStringListIterator strIterator(qstrList);
while (strIterator.hasNext())
qDebug() << strIterator.next() << endl;这里我们用QStringList添加QString字符串,用 <<来添加字符串,相当于Java中List的add()方法.
我们先学习一下QList的方法
1.增加字符串 append() QStringList可以通过append(),或使用<< 来添加List元素,如
qstrList.append("python");
qstrList << "PHP" ;2.插入字符串 insert() 插入字符串insert方法可以将字符串插入到我们指定的list的位置:
qstrList.insert(0,"C#");第一个参数为我们要插入的位置,后面的参数表示我们要插入的值。 3.替换字符串 replace() 通过replace()方法我们可以替换掉list中某一位置的值
qstrList.replace(0,"Web");第一个参数为我们要替换的位置,后面的参数表示我们要替换的值。 4.删除字符串 removeAt(), removeFirst(), removeLast(), and removeOne() 我们可以根据方法名,推断出个方法的具体作用,根据实际需要选着对应的方法: 这里说一下removeOne()方法,他表示删除一个具体的值,参数为我们list里的内容,其定义是: bool removeOne(const T &value) ,返回值为bool类型,我们写一下用法的例子
bool isflag = qstrList.removeOne("C#");//删除一个指定的元素,删除成功返回true,删除失败返回fasle
qDebug() << isflag << endl;
int n = qstrList.removeAll("Java");//删除所有的指定的元素,返回删除的元素个数
qDebug() << n << endl;
qstrList.removeAt(2);//删除第三个元素
qstrList.removeFirst();//删除第一个元素
qstrList.removeLast();//删除最后一个元素接下来我们学习一下QStringList的方法
1. 合并字符串使用join( )
QString str = fonts.join(",");
// str == "Android,Qt Creator,Java,C++"2. 拆分字符串
QString str = "Android,Qt Creator, ,Java,C++";
QStringList list1 = str.split(",");
// list1: [ "Android", "Qt Creator"," ", "Java", "C++" ]
QStringList list2 = str.split(",", QString::SkipEmptyParts);
// list2:[ "Android", "Qt Creator", "Java", "C++" ]也就是说如果有QString::SkipEmptyParts,空项不会出现在结果。默认情况下,空项被保留 3. 索引 IndexOf()函数返回给定字符串的第一个出现的索引。 而lastIndexOf()函数,返回字符串的最后一次出现的索引。
QStringList qstrList;
qstrList<<"Java" << "Android" << "Qt Creator" << "Java" << "C++";
int index = qstrList.indexOf("Java");//返回 0
int index = qstrList.indexOf("Java");//返回 34. 替换replaceInStrings()
QStringList files;
files << "$file/src/moc/moc.y" << "$file/src/moc/moc.l" << "$file/include/qconfig.h";
files.replaceInStrings("$file", "/usr/file");
// files: [ "/usr/file/src/moc/moc.y", ...]5. 过滤filter() 可以让你提取一个新的列表只包含这些字符串包含一个特定的字符串(或匹配特定正则表达式):
QStringList list;
list << "Bill Murray" << "John Doe" << "Bill Clinton";
QStringList result;
result = list.filter("Bill");
// result: ["Bill Murray", "Bill Clinton"]
//比较字符串的时候
//Qt::CaseSensitive 搜索区分大小写
//Qt::CaseInSensitive 不区分大小写
result = list.filter("bill",Qt::CaseInSensitive);
// result: ["Bill Murray", "Bill Clinton"]总结:
QStringList类提供字符串列表。QStringList继承自QList < QString >。与QList一样,QStringList是隐式共享的。它提供了快速的基于索引的访问,以及快速插入和删除。将字符串列表作为值参数传递是快速和安全的。QList的所有功能也适用于QStringList。例如,您可以使用isEmpty()来测试列表是否为空,您可以调用诸如append()、prepend()、insert()、replace()、removeAll()、removeAt()、removeFirst()和removeOne()来修改QStringList的函数。此外,QStringList提供了一些方便的函数,使字符串的处理列表变得更容易
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133033.html原文链接:https://javaforall.cn
边栏推荐
- 浙江大学课程攻略共享计划
- 如何找到外文文献对应的中文文献?
- 智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
- Const keyword and its function (usage), detailed explanation of C language const
- Is it safe for Huatai Securities to open an account online?
- [digital ic/fpga] detect the position of the last matching sequence
- Safe, fuel-efficient and environment-friendly camel AGM start stop battery is full of charm
- Teach you how to transplant tinyriscv to FPGA
- 系统学习+主动探索,是最舒适的入门学习方式!
- Excel print settings public header
猜你喜欢

安全省油環保 駱駝AGM啟停電池魅力十足

【tinyriscv verilator】分支移植到正点原子达芬奇开发板
![[try to hack] kill evaluation](/img/93/e623e25dc4dec1f656227c7651577e.png)
[try to hack] kill evaluation

ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码

Zero foundation self-study SQL course | case function

本地可视化工具连接阿里云centOS服务器的redis

【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer

Eliminate gaps around El image images

webserver流程图——搞懂webserver各模块间调用关系

Chenyun pytorch learning notes_ Build RESNET with 50 lines of code
随机推荐
ASP. Net warehouse purchase, sales and inventory ERP management system source code ERP applet source code
Cornernet由浅入深理解
[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer
手把手教你移植 tinyriscv 到FPGA上
MySQL企业级参数调优实践分享
通过中金证券经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
Zero foundation self-study SQL course | if function
One step forward is excellent, one step backward is ignorant
第 2 章 集成 MP
Golang uses Mongo driver operation - query (Advanced)
[tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom
[PCL self study: pclplotter] pclplotter draws data analysis chart
[AI application] detailed parameters of NVIDIA Tesla v100-pcie-32gb
Structure de stockage des graphiques
VMware virtual machine bridging connectivity
Zero foundation self-study SQL course | complete collection of date functions in SQL
[idea] idea formatting code skills
Using two stacks to implement queues [two first in first out is first in first out]
C WinForm reads the resources picture
[AI application] detailed parameters of Jetson Xavier nx