其他分享
首页 > 其他分享> > 字符串组合

字符串组合

作者:互联网

 

字符串组合:

 1 #include "dialog.h"  //用户自定义控件
 2 #include <QApplication>  //这个头文件是应用程序必须的
 3 #include <QDebug>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8 
 9     /*<字符串组合: + += >*/
10     QString str1 = "welcome ";
11     str1 + "to you! "; //QString类重载了 = 运算符
12     /*
13     inline QT_ASCII_CAST_WARN const QString operator+(const QString &s1, const char *s2)
14     当然,还有其他重载:比如
15     inline QT_ASCII_CAST_WARN const QString operator+(const char *s1, const QString &s2)
16     */
17 
18     qDebug()<< (str1 + "to you! ");  //输出结果,调试语句,头文件:<QDebug>
19 
20     QString str2 = "Hello, ";
21     str2 += "world! ";    //QString类重载了 += 运算符
22     qDebug()<< str2;
23 
24     /*<字符串组合:append()方法相当于 +=的作用 >*/
25     QString str3 = str1.append("to you! ");
26     qDebug()<< str3;
27 
28     /*<字符串组合:sprintf() >*/
29     QString str4;
30     str4.sprintf("%s",str1.data());  //str4 = "welcome "  inline const QChar *QString::data() const,返回类型 const char*字符串
31     str4.sprintf("%s %s",str1.data(),"to you! "); //str4 = "welcome to you! "
32     qDebug()<< str4;
33 
34     /*<字符串组合:QString::arg() >*/
35     QString str5;
36     str5 = QString("%1 was born in %2.").arg("John").arg(1998);
37     qDebug()<< str5;
38 
39     /*
40     insert() 字符串特定位置插入另一字符
41     prepend() 字符串开头插入另一个字符
42     replace() 替换字符
43     trimmed() 移除字符串两端的空格字符
44     */
45     return a.exec();
46 }

字符串查找:

 

 

字符串转换:

 

标签:const,组合,str1,str4,char,qDebug,QString,字符串
来源: https://www.cnblogs.com/data1213/p/10741431.html