Qt中的字符串类
采用unicode编码
使用隐式共享技术来节省内存和不必要的数据拷贝
跨平台使用,不必考虑字符串的平台兼容性
QString 直接支持字符串和数字的相互转换
QString 直接支持字符串的大小比较
QString 直接支持不同字符编码间的相互转换
QString 直接支持std::string 和std::wstring 的相互转换
QString 直接支持正在表达式的应用
#include <QCoreApplication>
#include <QDebug>
void Sample_1()
{
QString s = "add";
s.append(" ");
s.append("Qt");
s.prepend(" ");
s.prepend("C++");
qDebug() << s;
s.replace("add", "&");
qDebug() << s;
}
void Sample_2()
{
QString s = "";
int index = 0;
s.sprintf("%d. I'm %s, thank you!", 1, "Delphi Tand");
qDebug() << s;
index = s.indexOf(",");//逗号的下标
s = s.mid(0, index);//0-index子串
qDebug() << s;
index = s.indexOf(".");
s = s.mid(index + 1, s.length());
s = s.trimmed();//去掉前面空格
index = s.indexOf(" ");
s = s.mid(index + 1, s.length());
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(int argc, char *argv[])
{
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("HELLO")
};
Sample_3(company, 5);
for(int i = 0; i < 5; i++)
{
qDebug() << company[i];
}
return 0;
}