#include <QDebug>
void Sample_1()
{
QString s = "add";
s.append(" "); // "add " 在后面追加
s.append("Qt"); // "add Qt"
s.prepend(" "); // " add Qt"在前面追加
s.prepend("C++"); // "C++ add Qt"
qDebug() << s;
s.replace("add", "&"); // "C++ & Qt" 替換
qDebug() << s;
}
void Sample_2()
{
QString s = "";
int index = 0;
s.sprintf("%d. I'm %s, thank you!", 1, "Delphi Tang"); // "1. I'm Delphi Tang, thank you!"格式化字符串 S
qDebug() << s;
index = s.indexOf(",");//,所在的下標
s = s.mid(0, index); // "1. I'm Delphi Tang" 截取從 0 開始到index處即“,”處的字符串
qDebug() << s;
index = s.indexOf(".");
s = s.mid(index + 1, s.length()); // " I'm Delphi Tang"
s = s.trimmed(); // "I'm Delphi Tang"去除前面的空格符
qDebug() << s;
index = s.indexOf(" ");//第一個空格符出現的位置
s = s.mid(index + 1, s.length()); // "Delphi Tang"
qDebug() << s;
}
//按照字母順序對字符串進行排序
void Sample_3(QString* a, int len)
{
for(int i=0; i<len; i++)
{
for(int j=i+1; j<len; j++)
{
if( a[j] < a[i] )
{
QString tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
int main()
{
qDebug() << "Sample_1:";
Sample_1();
qDebug() << endl;
qDebug() << "Sample_2:";
Sample_2();
qDebug() << endl;
qDebug() << "Sample_3:";
QString company[5] =
{
QString("Oracle"),
QString("Borland"),
QString("Microsoft"),
QString("IBM"),
QString("D.T.Software")
};
Sample_3(company, 5);
for(int i=0; i<5; i++)
{
qDebug() << company[i];
}
return 0;
}