高密微网站建设,个人做美食视频网站,自媒体发稿,网站友好度目录
操作字符串
查询字符串
Qt 常见数据类型 操作字符串
创建一个控制台项目
#xff08;1#xff09;QString提供一个二元的 “” 操作符#xff0c;主要用于组合两个字符串。QString str1 Hello World 传递给QString一个 const char* 类型的ASCII字符串 “He…目录
操作字符串
查询字符串
Qt 常见数据类型 操作字符串
创建一个控制台项目
1QString提供一个二元的 “” 操作符主要用于组合两个字符串。QString str1 Hello World 传递给QString一个 const char* 类型的ASCII字符串 “Hello World” 它被解释为一个典型的以 \0 结尾的C类型字符串
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);// 1.QString提供二元 “” 操作符应用功能一样 “”QString str1 Hello;str1 str1 World;qDebug()str1; // 打印信息qDebug()qPrintable(str1); // 去掉双引号QString str2 12345;str2ABCDE;qDebug()qPrintable(str2);return a.exec();
}Qt中创建一个QCoreApplication对象的实例。
具体解释如下
QCoreApplication是Qt框架中的一个核心类用于处理应用程序的事件循环和基本功能。a是一个QCoreApplication对象的实例通过调用构造函数QCoreApplication(int argc, char **argv)创建。argc是命令行参数的数量通常是程序启动时通过命令行传递的参数的数量。argv是命令行参数的值是一个指向字符串数组的指针每个元素表示一个命令行参数的字符串。
通过调用exec()函数Qt应用程序进入事件循环开始处理用户输入、定时器事件、网络通信等各种事件并按照信号和槽的连接关系执行相应的槽函数。
最后通过return语句将exec()函数的返回值返回可以在需要时获取事件循环的退出状态。 2QString::append() 函数具备与 “ 操作符同样的功能直接在一个字符串末尾添加另一个字符串。
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);// 2.QString::append()函数QString str1 Good;QString str2 bye;str1.append(str2); // str1 Good byeqDebug()qPrintable(str1);return a.exec();
}3组合字符串QString::sprintf()其实它跟 C 库当中 sprintf() 函数一样
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);// 3.QString::sprintf()函数QString strtemp;strtemp.sprintf(%s %s,Hello World!,Goodbye);qDebug()qPrintable(strtemp);return a.exec();
}4字符串组合方式 QString::arg() 函数该函数的重载可以处理多种数据类型。因为它类型齐全同时支持 Unicode可以改变 %n 参数顺序。
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QString strTemp;strTempQString(%1 was born in %2).arg(Sunny).arg(2000);qDebug()strTemp;return a.exec();
}查询字符串
1函数 QString::startsWith() 判断一个字符串是否以某个字符串开头。Qt::CaseInsensitive 代表大小写不敏感Qt::CaseSensitive 表示大小写敏感。对应关系函数 QString::endsWith().
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QString strTempHow are you;qDebug()strTemp.startsWith(How,Qt::CaseSensitive); // trueqDebug()strTemp.startsWith(are,Qt::CaseInsensitive); // falsereturn a.exec();
}2函数QString::contains() 判断一个指定的字符串是否出现过
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QString strTempHow are you;qDebug()strTemp.contains(How,Qt::CaseSensitive); // truereturn a.exec();
}3QString::toInt() 函数将字符串转换为整型数值toDouble()/toFloat()/toLong()等等
#include QCoreApplication#include QDebugint main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QString str25;bool isloop; // 判断是否成功int hexstr.toInt(isloop,16);qDebug()isloopisloop,hexhexendl;return a.exec();
}4QString::compare() 函数对两个字符串进行比较
#include QCoreApplication#include QDebug
#include iostreamusing namespace std;int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);int a1QString::compare(abcd,ABCD,Qt::CaseInsensitive);int b1QString::compare(about,Cat,Qt::CaseSensitive);int c1QString::compare(abcd,Cat,Qt::CaseInsensitive);couta1a1b1b1c1c1endl;return a.exec();
}5Qt将QString转换成ASCII
#include QCoreApplication#include QDebug
#include iostreamusing namespace std;int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QString strABC abc;QByteArray bytesstr.toUtf8();for(int i0;istr.size();i)qDebug()int(bytes.at(i));return a.exec();
}Qt 常见数据类型
注意定义在 #include QtGlobal #include QCoreApplication#include QDebug
#include iostream
#include QDateTimeusing namespace std;int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QDateTime dt;QString strDTdt.currentDateTime().toString(yyyy-MM-dd hh:mm:ss);qDebug()strDTendl;QByteArray a1(Qt Creator Hello World.);QByteArray b1a1.toLower(); // 将字符串大写字母转小写小写不变qDebug()b1endl;QByteArray c1a1.toUpper();qDebug()c1endl;return a.exec();
}