当前位置:网站首页>QT QString详解
QT QString详解
2022-07-28 15:10:00 【icewst】
文章目录
一些描述:
QString 类是 Qt 中用于表示字符串的类,实现在 QtCore 共享库中。QString 类在实现上有以下特征。
- 字符串采用 Unicode 内部编码,可以表示世界上大多数语言的文字; 字符串的存储有引用计数,当一个 QString 对象被复制为另一个
- QString 对象时,它们实际上指向相同的存储空间,仅仅是增加一个引用计数; 采用 “按需复制” 的技术,当指向相同存储空间的多个
- QString 对象中的一个要被修改时,将真正复制一个新的字符串并进行修改。
1.创建
QString str1 = "Welcome";
//下面这种写法错误!!!要初始化后,才能加!
QString str2 = "Welcome"+" OK";
//正确
QString str3="Welcome";
str3=str3+"Hello";
2.连接拼接
QString abc="sss";
abc="xxx"+abc+"123"+abc;
3.比较
QString str="hello" ;
if(str =="hello")
{
qDebug("xiangtong");
}
结果为xiangtong
//返回int类型值,等于0表示相同,不等于0不同
//Qt::CaseInsensitive 这个类型,表示大小写不敏感 Qt::CaseSensitive 表示大小写敏感
QString str="Hello" ;
if(str.compare("hello",Qt::CaseInsensitive)==0)
{
qDebug("xiangtong");
}
结果为xiangtong
4.是否为空字符串
QString str;
if(str==NULL){
qDebug("kong");
}
if(str==""){
qDebug("kong");
}
QString str1="";
if(str1==""){
qDebug("kong");
}
三者结果都为kong
5.转换大小写
QString str="Hello" ;
str=str.toLower();
qDebug(str.toLatin1()); //这里输出为: hello
str=str.toUpper();
qDebug(str.toLatin1()); //这里输出为: HELLO
6.获取长度
QString str="Hello" ;
int len=str.length();
qDebug("%d",len); //这里输出结果为:5
7.去掉空格
//trimmed只能去掉首尾的空格
QString str=" He llo " ;
str=str.trimmed();
qDebug(str.toLatin1()); //输出结果:He llo
//用这种方法,才能去掉全部空格
QString str1=" He llo " ;
str1=str1.remove(QRegExp("\\s"));
qDebug(str1.toLatin1());//输出结果:Hello
8.获取子串
//mid的第一个参数 position 是子字符串的起始位置
//mid的第二个参数参数 n 是字符串的个数
// 如果n 为 -1,则表示一直到原字符串的结尾。
QString str="Hello" ;
QString str1=str.mid(0,2);
qDebug(str1.toLatin1());//输出结果为:He
9.查找字符串
//默认比较,大小写敏感
QString str="Hello" ;
if(str.contains("hel")){
qDebug("yes");
}else{
qDebug("no");
}
//输出结果为:no
//大小写不敏感的比较
QString str="Hello" ;
if(str.contains("hel",Qt::CaseInsensitive)){
qDebug("yes");
}
//输出结果为:yes CaseInsensitive表示大小写不敏感
10.替换字符串
QString str="Hello" ;
str=str.replace("llo","xxx");
qDebug(str.toLatin1()); //输出结果为:Hexxx
11.和char*之间的转换
//char*转QString
char charArry[] = "hello";
char* ch = charArry;
QString str = Qstring::fromUtf8(ch);
//QString转char*,先转为string,避免中文乱码
QString filename="he中国人llo";
std::string str = filename.toStdString();
const char* ch = str.c_str();
qDebug(ch);
//QString转char*
QString str= "he中国人llo";
char* ch ;
QByteArray ba = str.toUtf8();//这句避免中文乱码,这句和下面那句不能连着写
ch=ba.data();
qDebug(ch);
12.字符串和数值之间的转换
QString str="20";
int strInt=str.toInt();
int num=100;
QString str= QString::number(num);
注意事项:
- 中文做比较的时候
- 中文做和char* 之间的转换的时候
边栏推荐
- 2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161
- Record Clearfix clear float
- [live broadcast reservation] a new challenge under the evolution of data architecture - Shanghai railway station
- 比例电磁阀控制阀4-20mA转0-165mA/330mA信号隔离放大器
- 阿里云的rds mysql 只读实例在哪里创建
- Ethernet to RS485 serial port counter WiFi module LED light controller ibf165
- SDL2 简明教程(四):用 SDL_IMAGE 库导入图片
- Vm501 development kit development version single vibrating wire sensor acquisition module geotechnical engineering monitoring
- Two special functions (arrow function and method)
- Ask if you don't understand, and quickly become an advanced player of container service!
猜你喜欢

MicTR01 Tester 开发套件(振弦采集读数仪)使用说明

远距离串口服务器( 适配器)UART 转 1-Wire 应用

Instructions for mictr01 Tester development kit (vibrating wire acquisition reader)

不懂就问,快速成为容器服务进阶玩家!

小程序中的分页查询

mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?

js 栈

LabVIEW Linx toolkit controls Arduino equipment (expansion-1)

Advantages of optical rain gauge over tipping bucket rain gauge

Laser rangefinder non-contact surface crack monitor
随机推荐
管理区解耦架构见过吗?能帮客户搞定大难题的
2021-10-21 notes
Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
js 数组(总结)
1路编码器2路DI转速测量RS485串口连接1路DO报警模块IBF151
Vm501 development kit development version single vibrating wire sensor acquisition module geotechnical engineering monitoring
Using SYSTEMd to manage services
Pyqt5 rapid development and practice 5.2 container: load more controls
Advantages of optical rain gauge over tipping bucket rain gauge
2路DI高速脉冲计数器1路编码器转Modbus TCP有线无线模块IBF161
光学雨量计应用降雨量检测
头条文章_signature
JS linked list 02
js 链表 01
Rust Getting Started Guide (rustup, cargo)
2021 Yahong pen test questions
Basic structure and operation principle of solar street lamp
为什么学编程的人大多数都去了深圳和北京?
电压频率的变换原理
Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)