当前位置:网站首页>Detailed explanation of QT qstring
Detailed explanation of QT qstring
2022-07-28 16:25:00 【icewst】
List of articles
Some descriptions :
QString Class is Qt Class used to represent strings in , Realize in QtCore In the shared library .QString Class has the following characteristics in implementation .
- String adoption Unicode Internal encoding , Words that can express most languages in the world ; The string is stored with a reference count , When one QString Object is copied as another
- QString Object time , They actually point to the same storage space , Just add a reference count ; use “ Copy on demand ” Technology , When pointing to multiple of the same storage space
- QString When one of the objects is to be modified , Will actually copy a new string and modify it .
1. establish
QString str1 = "Welcome";
// The following is wrong !!! After initialization , To add !
QString str2 = "Welcome"+" OK";
// correct
QString str3="Welcome";
str3=str3+"Hello";
2. Connection splicing
QString abc="sss";
abc="xxx"+abc+"123"+abc;
3. Compare
QString str="hello" ;
if(str =="hello")
{
qDebug("xiangtong");
}
The result is xiangtong
// return int Type values , be equal to 0 It means the same , It's not equal to 0 Different
//Qt::CaseInsensitive This type , Indicates case insensitive Qt::CaseSensitive Indicates case sensitivity
QString str="Hello" ;
if(str.compare("hello",Qt::CaseInsensitive)==0)
{
qDebug("xiangtong");
}
The result is xiangtong
4. Is it an empty string
QString str;
if(str==NULL){
qDebug("kong");
}
if(str==""){
qDebug("kong");
}
QString str1="";
if(str1==""){
qDebug("kong");
}
The results of all three are kong
5. Convert case
QString str="Hello" ;
str=str.toLower();
qDebug(str.toLatin1()); // The output here is : hello
str=str.toUpper();
qDebug(str.toLatin1()); // The output here is : HELLO
6. To obtain the length of the
QString str="Hello" ;
int len=str.length();
qDebug("%d",len); // The output here is :5
7. Remove space
//trimmed Only remove the spaces at the beginning and end
QString str=" He llo " ;
str=str.trimmed();
qDebug(str.toLatin1()); // Output results :He llo
// by this means , To remove all spaces
QString str1=" He llo " ;
str1=str1.remove(QRegExp("\\s"));
qDebug(str1.toLatin1());// Output results :Hello
8. Get substring
//mid The first parameter of position Is the starting position of the substring
//mid The second parameter of n Is the number of strings
// If n by -1, It means to the end of the original string .
QString str="Hello" ;
QString str1=str.mid(0,2);
qDebug(str1.toLatin1());// The output is :He
9. Find string
// Default comparison , Case sensitive
QString str="Hello" ;
if(str.contains("hel")){
qDebug("yes");
}else{
qDebug("no");
}
// The output is :no
// Case insensitive comparison
QString str="Hello" ;
if(str.contains("hel",Qt::CaseInsensitive)){
qDebug("yes");
}
// The output is :yes CaseInsensitive Indicates case insensitive
10. Replace string
QString str="Hello" ;
str=str.replace("llo","xxx");
qDebug(str.toLatin1()); // The output is :Hexxx
11. and char* Conversion between
//char* turn QString
char charArry[] = "hello";
char* ch = charArry;
QString str = Qstring::fromUtf8(ch);
//QString turn char*, First to string, Avoid miscoding in Chinese
QString filename="he Chinese llo";
std::string str = filename.toStdString();
const char* ch = str.c_str();
qDebug(ch);
//QString turn char*
QString str= "he Chinese llo";
char* ch ;
QByteArray ba = str.toUtf8();// This sentence avoids Chinese garbled , This sentence and the following sentence cannot be written together
ch=ba.data();
qDebug(ch);
12. Conversion between string and numeric value
QString str="20";
int strInt=str.toInt();
int num=100;
QString str= QString::number(num);
matters needing attention :
- When comparing Chinese
- Chinese do and char* When switching between
边栏推荐
- Basic structure and operation principle of solar street lamp
- MicTR01 Tester 开发套件(振弦采集读数仪)使用说明
- 2021-10-21 notes
- JS array (summary)
- 五舅的思考
- 2021 Kent interview question 3
- 高精度绝对角度传感器应用高速度角度监测
- I'll show you a little chat! Summary of single merchant function modules
- SDL2 简明教程(四):用 SDL_IMAGE 库导入图片
- Writing of factorial
猜你喜欢

解决电脑恶意广告弹窗的思路

LwIP development | realize TCP server through socket

Zhengda cup hacker marathon data analysis competition

使用js直传oss阿里云存储文件,解决大文件上传服务器限制

两种特殊函数(箭头函数和方法)

Ethernet to RS485 serial port counter WiFi module LED light controller ibf165

Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)

The epidemic dividend disappeared, and the "home fitness" foam dissipated

光学雨量计对比翻斗式雨量计的优势

A good start
随机推荐
资本「断供」两年,我只能把公司卖了
高精度绝对角度传感器应用高速度角度监测
Automatically pack compressed backup download and delete bat script commands
激光测距仪非接触式地表裂缝监测仪
深部位移监测系统wk813应用边坡、大坝、堤防、铁路和建筑基坑开挖等深部位移测量
I came across Digital Phoenix coordinate Xuhui Meiluo city in Shanghai
Notes on October 22, 2021
JS bidirectional linked list 01
振弦采集模块测量振弦传感器的流程步骤?
Rust Getting Started Guide (rustup, cargo)
远距离串口服务器( 适配器)UART/I2C/1-Wire/SPI PS304常见问题及注意事项
A tour of grp:05 - GRP server streaming service end stream
laravel
Summary of for loop in JS
远距离串口服务器( 适配器)UART 转 1-Wire 应用
Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
I can only sell the company after the capital has been "cut off" for two years
Application of optical rain gauge to rainfall detection
两种特殊函数(箭头函数和方法)
2021 Yahong pen test question 2