在介绍内容之前,先说明一下开发环境,如下图:
Qt版本:Qt5.3.2;
Qt开发工具:Qt Creater 3.2.1;
Qt构建工具:Desktop Qt 5.3 MinGW 32bit;
Qt开发平台:Windows 7 64bit。
在介绍QString的使用之前,先介绍一下QString的编码方式。QString采用Unicode编码方式,即:每一个字符用两个字节来表示,当然这里的每一个字符不是传统C字符串中的ASCII字符,它涵盖了更多的字符,比如:中文、俄文、日文等等。
Unicode是当前国际标准的编码字符集,支持大多数的写入系统,它是US-ASCII (ANSI X3.4-1986)字符集和Latin-1 (ISO 8859-1)字符集的超集。
在学习QString之前,将QString的接口分为初始化、增加、删除、修改、查询、遍历、子字符串、格式化、比较和转换共10类,这样比较利于学习。由于QString的接口太多了,这里将要学习的接口一并列出来,有个初步的认识,今天只介绍初始化的部分,后续会在博客中一类一类的介绍。
下面是这10类的接口。
//
//初始化
//
QString();
QString(const QChar * unicode, int size = -1);
QString(QChar ch);
QString(int size, QChar ch);
QString(const QString & other);
QString(const char * str);
QString & operator=(const char * str);
//
//增加
//
QString & append(const QString &str);
QString & append(const QChar *str, int len);
QString & append(QChar ch);
QString & prepend(const QString &str);
QString & prepend(QChar ch);
void push_back(const QString &str);
void push_back(QChar ch);
void push_front(const QString &str);
void push_front(QChar ch);
QString & insert(int position, const QString &str);
QString & insert(int position, const QChar *unicode, int size);
QString & insert(int position, QChar ch);
QString & operator+=(const QString &str);
QString & operator+=(QChar ch);
//
//删除
//
void clear();
void chop(int n);
QString & remove(int position, int n);
QString & remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
QString & remove(const QString &str, Qt::caseSensitivity cs = Qt::CaseSensitive);
//
//修改
//
QString & fill(QChar ch, int size = -1);
QString & replace(int position, int n, const QString &after);
QString & replace(int position, int n, const QChar *unicode, int size);
QString & replace(int position, int n, QChar after);
QString & replace(const QString &before, const QString &after, Qt::CaseSensitivity cs);
QString & replace(QChar ch, const QString &after, Qt::CaseSensitivity cs);
QString & replace(QChar before, QChar after, Qt::CaseSensitivity);
void truncate(int position);
QString trimmed();
QString & operator+=(const QString &other);
QString & operator+=(QChar ch);
//
//查询
//
bool contains(const QString &str, Qt::CaseSensitivity cs);
bool contains(QChar ch, Qt::CaseSensitivity cs);
bool endsWith(const QString &s, Qt::CaseSensitivity cs);
bool endsWith(QChar c, Qt::CaseSensitivity cs);
int indexOf(const QString &str, int from, Qt::CaseSensitivity cs);
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs);
int lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs);
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs);
bool startsWith(const QString &s, Qt::CaseSensitivity cs);
bool startsWith(QChar ch, Qt::CaseSensitivity cs);
//
//遍历
//
const_iterator cbegin();
const_iterator cend();
const QChar at(int position) const;
QCharRef operator[](int position);
const QChar operator[](int position) const;
//
//子字符串
//
QString left(int n);
QString mid(int position, int n);
QString right(int n);
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs);
QStringList split(QChar sep, SplitBehavior behavior, Qt::CaseSensitivity cs);
//
//格式化
//
一系列arg(...)的重载函数,可以再Qt Creater的帮助中,搜索QString关键字,查看关于arg()的函数及说明
//
//比较
//
int compare(const QString &other, Qt::CaseSensitivity cs);
int localeAwareCompare(const QString &other);
bool operator==(const char *other);
bool operator<(const char *other);
bool operator<=(const char *other);
bool operator>(const char *other);
bool operator>=(const char *other);
//
//转换
//
//数字转换
QString & setNum(int n, int base = 10);
QString & setNum(uint n, int base = 10);
QString & setNum(long n, int base = 10);
QString & setNum(ulong n, int base = 10);
QString & setNum(qlonglong n, int base = 10);
QString & setNum(qulonglong n, int base = 10);
QString & setNum(short n, int base = 10);
QString & setNum(ushort n, int base = 10);
QString & setNum(double n, char format = 'g', int precision = 6);
QString & setNum(float n, char format = 'g', int precision = 6);
short toShort(bool * ok = 0, int base = 10) const;
int toInt(bool * ok = 0, int base = 10) const;
long toLong(bool * ok = 0, int base = 10) const;
qlonglong toLongLong(bool * ok = 0, int base = 10) const;
ushort toUShort(bool * ok = 0, int base = 10) const;
uint toUInt(bool * ok = 0, int base = 10) const;
ulong toULong(bool * ok = 0, int base = 10) const;
qulonglong toULongLong(bool * ok = 0, int base = 10) const;
float toFloat(bool * ok = 0) const;
double toDouble(bool * ok = 0) const;
QString QString::number(long n, int base = 10);
QString QString::number(double n, char format = 'g', int precision = 6);
QString QString::number(ulong n, int base = 10);
QString QString::number(int n, int base = 10);
QString QString::number(uint n, int base = 10);
QString QString::number(qlonglong n, int base = 10);
QString QString::number(qulonglong n, int base = 10);
//编码转换
QString & setRawData(const QChar * unicode, int size);
QString & setUnicode(const QChar * unicode, int size);
QString & setUtf16(const ushort * unicode, int size);
QByteArray toUtf8() const;
QVector toUcs4() const;
std::string toStdString() const;
std::wstring toStdWString() const;
int toWCharArray(wchar_t * array) const;
const QChar * unicode() const;
const ushort * utf16() const;
QString QString::fromRawData(const QChar * unicode, int size);
QString QString::fromStdString(const std::string & str);
QString QString::fromStdWString(const std::wstring & str);
QString QString::fromUcs4(const uint * unicode, int size = -1);
QString QString::fromUtf8(const char * str, int size = -1);
QString QString::fromUtf8(const QByteArray & str);
QString QString::fromUtf16(const ushort * unicode, int size = -1);
QString QString::fromWCharArray(const wchar_t * string, int size = -1);
//大小写转换
QString toLower() const;
QString toUpper() const;
下面介绍初始化接口的使用例程。
qDebug("--- string: 初始化 ---");
/*
* QString()
* QString(const QChar * unicode, int size = -1)
* QString(QChar ch)
* QString(int size, QChar ch)
* QString(const QString & other)
* QString(const char * str)
* operator=(const char * str)
*/
//QString
QString str1;
qDebug("str1 is empty? %s", str1.isEmpty() ? "yes" : "no");
//QString(const QChar * unicode, int size = -1)
QChar *pCharNull = NULL;
QChar pCharNormal[10] = { '1', '2', '3', '4', '5', NASCII_UC("一"), NASCII_UC("二"), NASCII_UC("三"), NASCII_UC("四"), NASCII_UC("五") };
QChar pCharNullTerminated[11] = { '1', '2', '3', '4', '5', NASCII_UC("一"), NASCII_UC("二"), NASCII_UC("三"), NASCII_UC("四"), NASCII_UC("五"), 0 };
QString str2(pCharNull); //If unicode is 0, a null string is constructed.
QString str3(pCharNormal, 10);
QString str4(pCharNullTerminated); //If size is negative, unicode is assumed to point to
//a nul-terminated array and its length is determined
//dynamically. The terminating nul-character is not
//considered part of the string.
qDebug("str2 is empty? %s", str2.isEmpty() ? "yes" : "no");
qDebug("str3 = %s, length = %d", qPrintableU8(str3), str3.length());
qDebug("str4 = %s, length = %d", qPrintableU8(str4), str4.length());
QMessageBox::question(this, "问题 - 标题", str4, "是", "否", "忽略", 0, 2);
//QString(QChar ch)
QString str5(pCharNormal[5]);
qDebug("str5 = %s, unicode = 0x%04X", qPrintableU8(str5), NASCII_UC(str5));
//QString(int size, QChar ch)
QString str6(10, pCharNormal[9]);
qDebug("str6 = %s, length = %d", qPrintableU8(str6), str6.length());
//QString(const QString &string)
//QString(const char * str)
//operator=(const char * str)
QString str7 = "12345一二三四五";
QString str8("12345一二三四五");
QString str9(str7);
qDebug("str7 = %s, length = %d", qPrintableU8(str7), str7.length());
qDebug("str8 = %s, length = %d", qPrintableU8(str8), str8.length());
qDebug("str9 = %s, length = %d", qPrintableU8(str9), str9.length());
在学习过程中,可以将上述代码放到一个按钮的响应函数中,以调试方式运行,就可以在Qt Creater中的应用程序输出窗口看到输出结果了。