当前位置:网站首页>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
边栏推荐
- Stm32f103c8t6 + 0.96 "I2C OLED display 3d_cube
- JS array (summary)
- js中的for循环总结
- RF module wireless transceiver rf63u chip application data transmission and infrastructure network
- 加速投资的小红书,“病急乱投医”?
- JS priority queue
- 使用py,根据日志记录自动生成周报
- 远距离串口服务器( 适配器)UART 转 1-Wire 应用
- 一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi
- 1. Simple command line connection to database
猜你喜欢
随机推荐
R language ggplot2 visually draws line plots, and uses gghighlight package to highlight the lines that meet the combination judgment conditions in the line graphs (satisfies both condition a and b)
【Multisim仿真】LM339过零电路仿真
加速投资的小红书,“病急乱投医”?
带你来浅聊一下!单商户功能模块汇总
不懂就问,快速成为容器服务进阶玩家!
mysql 查看事件状态语句和修改办法
为什么学编程的人大多数都去了深圳和北京?
高精度绝对角度传感器应用高速度角度监测
Two special functions (arrow function and method)
使用py,根据日志记录自动生成周报
Laser rangefinder non-contact surface crack monitor
Have you seen the management area decoupling architecture? Can help customers solve big problems
Implementation of skip table
Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
PHP mb_ Substr Chinese garbled code
远距离串口服务器( 适配器)UART 转 1-Wire 应用
R language uses ggpairs function of ggally package to visualize pairwise relationship graph of grouping multivariable, set alpha parameter to change image transparency, diagonal continuous variable de
flashfxp 530 User cannot log in. ftp
Rust Getting Started Guide (crite Management)
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书









