QString stores a string of 16-bit QChars, where each QChar corresponds one Unicode 4.0 character.
一、初始化QString字符串
1)Char * 初始化
QString str = "Hello";
QString converts the const char * data into Unicode using the fromAscii() function.
2)QChar[]数组初始化
- static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
- QString str(data, 4);
static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
QString str(data, 4);
也可以这样
- QRegExp pattern;
- static const QChar unicode[] = { 0x005A, 0x007F, 0x00A4, 0x0060,
- 0x1009, 0x0020, 0x0020};
- int size = sizeof(unicode) / sizeof(QChar);
- QString str = QString::fromRawData(unicode, size);
QRegExp pattern;
static const QChar unicode[] = { 0x005A, 0x007F, 0x00A4, 0x0060,
0x1009, 0x0020, 0x0020};
int size = sizeof(unicode) / sizeof(QChar);
QString str = QString::fromRawData(unicode, size);
3)还可以通过risize()函数和每个QChar字符进行初始化
- QString str;
- str.resize(4);
- str[0] = QChar('U');
- str[1] = QChar('n');
- str[2] = QChar(0x10e3);
- str[3] = QChar(0x03a3);
QString str;
str.resize(4);
str[0] = QChar('U');
str[1] = QChar('n');
str[2] = QChar(0x10e3);
str[3] = QChar(0x03a3);
通过at()函数进行每个字符读取
- QString str;
- for (int i = 0; i < str.size(); ++i) {
- if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f'))
- qDebug() << "Found character in range [a-f]";
- }
QString str;
for (int i = 0; i < str.size(); ++i) {
if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f'))
qDebug() << "Found character in range [a-f]";
}
二、QString的操作函数 后续慢慢更新
- QString str = "and";
- str.prepend("rock "); // str == "rock and"
- str.append(" roll"); // str == "rock and roll"
- str.replace(5, 3, "&"); // str == "rock & roll"
- QString x = "free";
- QString y = "dom";
- x.append(y); // x == "freedom"
- x.insert(x.size(), y);//x == "freedom"
- QString str = "Meal";
- str.insert(1, QString("ontr"));
- // str == "Montreal"
- QString s = "Montreal";
- s.remove(1, 4); // s == "Meal" 移除1-4字符
- QString str("LOGOUT\r\n");
- str.chop(2); // str == "LOGOUT" 从字符串后面移除2字符
- QString str = "Vladivostok";
- str.truncate(4); // str == "Vlad" 到4截断字符
- QString s = "Hello world";
- s.resize(5); // s == "Hello"
- s.resize(8); // s == "Hello???" (where ? stands for any character)
- QString t = "Hello";
- t += QString(10, 'X');
- // t == "HelloXXXXXXXXXX"
- QString r = "Hello";
- r = r.leftJustified(10, ' ');
- // r == "Hello "
- QString x = "Pineapple";
- QString y = x.left(4); // y == "Pine"
- QString x = "Pineapple";
- QString y = x.right(5); // y == "apple"
- QString str = "Pineapple";
- str = str.rightJustified(5, '.', true); // str == "Pinea"
- QString str = "Bananas";
- str.endsWith("anas"); // returns true
- str.endsWith("pple"); // returns false
- QString str = "Berlin";
- str.fill('z');
- // str == "zzzzzz"
- str.fill('A', 2);
- // str == "AA"
- QString x = "sticky question";
- QString y = "sti";
- x.indexOf(y); // returns 0
- x.indexOf(y, 1); // returns 10
- x.indexOf(y, 10); // returns 10
- x.indexOf(y, 11); // returns -1
- QString x = "crazy azimuths";
- QString y = "az";
- x.lastIndexOf(y); // returns 6
- x.lastIndexOf(y, 6); // returns 6
- x.lastIndexOf(y, 5); // returns 2
- x.lastIndexOf(y, 1); // returns -1
- QString x = "Nine pineapples";
- QString y = x.mid(5, 4); // y == "pine"
- QString z = x.mid(5); // z == "pineapples"
- long a = 63;
- QString s = QString::number(a, 16); // s == "3f"
- QString t = QString::number(a, 16).toUpper(); // t == "3F"
- QString s = "Montreal";
- s.remove(1, 4);
- // s == "Meal"
- QString t = "Ali Baba";
- t.remove(QChar('a'), Qt::CaseInsensitive);
- // t == "li Bb"
- QString x = "Say yes!";
- QString y = "no";
- x.replace(4, 3, y);
- // x == "Say no!"
- QString str = "colour behaviour flavour neighbour";
- str.replace(QString("ou"), QString("o"));
- // str == "color behavior flavor neighbor"
- QString str;
- //! [51]
- QString csv = "forename,middlename,surname,phone";
- QString path = "/usr/local/bin/myapp"; // First field is empty
- QString::SectionFlag flag = QString::SectionSkipEmpty;
- str = csv.section(',', 2, 2); // str == "surname"
- str = path.section('/', 3, 4); // str == "bin/myapp"
- str = path.section('/', 3, 3, flag); // str == "myapp"
- //字符串转换成整型
- QString str;
- str.setNum(1234); // str == "1234"
- //字符串转成整型
- long a = 63;
- QString s = QString::number(a, 16); // s == "3f"
- QString t = QString::number(a, 16).toUpper(); // t == "3F"
- QString str = "a,,b,c";
- QStringList list1 = str.split(",");
- // list1: [ "a", "", "b", "c" ]
- QStringList list2 = str.split(",", QString::SkipEmptyParts);
- // list2: [ "a", "b", "c" ]
- QString str = " lots\t of\nwhitespace\r\n ";
- str = str.trimmed();
- // str == "lots\t of\nwhitespace"
- //判断字符串相等
- int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0
- int y = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0
- int z = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0
- Qstring 转换char*问题!
- 方法一:
- QString qstr("hello,word");
- const char * p = qstr.toLocal8Bit().data();
- 方法二:
- const char *p = qstr.toStdString().data();
- //转换过来的是常量
- 方法三:
- QString str="abc";
- char *ch;
- QByteArray ba = str.toLatin1()
- ch = ba.data();
QString str = "and";
str.prepend("rock "); // str == "rock and"
str.append(" roll"); // str == "rock and roll"
str.replace(5, 3, "&"); // str == "rock & roll"
QString x = "free";
QString y = "dom";
x.append(y); // x == "freedom"
x.insert(x.size(), y);//x == "freedom"
QString str = "Meal";
str.insert(1, QString("ontr"));
// str == "Montreal"
QString s = "Montreal";
s.remove(1, 4); // s == "Meal" 移除1-4字符
QString str("LOGOUT\r\n");
str.chop(2); // str == "LOGOUT" 从字符串后面移除2字符
QString str = "Vladivostok";
str.truncate(4); // str == "Vlad" 到4截断字符
QString s = "Hello world";
s.resize(5); // s == "Hello"
s.resize(8); // s == "Hello???" (where ? stands for any character)
QString t = "Hello";
t += QString(10, 'X');
// t == "HelloXXXXXXXXXX"
QString r = "Hello";
r = r.leftJustified(10, ' ');
// r == "Hello "
QString x = "Pineapple";
QString y = x.left(4); // y == "Pine"
QString x = "Pineapple";
QString y = x.right(5); // y == "apple"
QString str = "Pineapple";
str = str.rightJustified(5, '.', true); // str == "Pinea"
QString str = "Bananas";
str.endsWith("anas"); // returns true
str.endsWith("pple"); // returns false
QString str = "Berlin";
str.fill('z');
// str == "zzzzzz"
str.fill('A', 2);
// str == "AA"
QString x = "sticky question";
QString y = "sti";
x.indexOf(y); // returns 0
x.indexOf(y, 1); // returns 10
x.indexOf(y, 10); // returns 10
x.indexOf(y, 11); // returns -1
QString x = "crazy azimuths";
QString y = "az";
x.lastIndexOf(y); // returns 6
x.lastIndexOf(y, 6); // returns 6
x.lastIndexOf(y, 5); // returns 2
x.lastIndexOf(y, 1); // returns -1
QString x = "Nine pineapples";
QString y = x.mid(5, 4); // y == "pine"
QString z = x.mid(5); // z == "pineapples"
long a = 63;
QString s = QString::number(a, 16); // s == "3f"
QString t = QString::number(a, 16).toUpper(); // t == "3F"
QString s = "Montreal";
s.remove(1, 4);
// s == "Meal"
QString t = "Ali Baba";
t.remove(QChar('a'), Qt::CaseInsensitive);
// t == "li Bb"
QString x = "Say yes!";
QString y = "no";
x.replace(4, 3, y);
// x == "Say no!"
QString str = "colour behaviour flavour neighbour";
str.replace(QString("ou"), QString("o"));
// str == "color behavior flavor neighbor"
QString str;
//! [51]
QString csv = "forename,middlename,surname,phone";
QString path = "/usr/local/bin/myapp"; // First field is empty
QString::SectionFlag flag = QString::SectionSkipEmpty;
str = csv.section(',', 2, 2); // str == "surname"
str = path.section('/', 3, 4); // str == "bin/myapp"
str = path.section('/', 3, 3, flag); // str == "myapp"
//字符串转换成整型
QString str;
str.setNum(1234); // str == "1234"
//字符串转成整型
long a = 63;
QString s = QString::number(a, 16); // s == "3f"
QString t = QString::number(a, 16).toUpper(); // t == "3F"
QString str = "a,,b,c";
QStringList list1 = str.split(",");
// list1: [ "a", "", "b", "c" ]
QStringList list2 = str.split(",", QString::SkipEmptyParts);
// list2: [ "a", "b", "c" ]
QString str = " lots\t of\nwhitespace\r\n ";
str = str.trimmed();
// str == "lots\t of\nwhitespace"
//判断字符串相等
int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0
int y = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0
int z = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0
Qstring 转换char*问题!
方法一:
QString qstr("hello,word");
const char * p = qstr.toLocal8Bit().data();
方法二:
const char *p = qstr.toStdString().data();
//转换过来的是常量
方法三:
QString str="abc";
char *ch;
QByteArray ba = str.toLatin1()
ch = ba.data();
三、更有效的构造 String
我们通常通过"+"连接多个子字符串,例如:
QString foo;
QString type = "long";
foo->setText(QLatin1String("vector<") + type + QLatin1String(">::iterator"));
if (foo.startsWith("(" + type + ") 0x"))
...
这种构造写法,没有任何错误,但是有一些隐藏的效率问题。从Qt4.6之后,我们就可以消除这些问题。
首先,多次使用"+"意味着多次进行内存分配。当你连接n个子字符串的时候,如果n>2,就会有n-1次内存分配。
其次,QLatin1String 并不在内存中保存它的长度,而是当你需要知道它的长度时候,直接调用qstrlen()函数。
在Qt4.6中,新建了一个内部模板类QStringBuilder ,它有一些可以帮助解决该问题的方法。这个类被当做内部的,并不出现在我们的帮助文档中,因为你的代码中并不会用到它。它的使用将会自动的,下面详细说一下。
QStringBuilder 使用表达式模板重新解释'%'操作符,这样当你使用'%'代替'+'符号,连接字符串的时候,多次连接子字符串将被推迟,直到最后整个字符串被确定。这个时候,这个字符串的大小就是已知的了。这个时候内存分配器,就会一次性的分配这个已知大小的空间,然后把各个子字符串一个个拷贝进来。
QLatin1Literal 是一个二级的内部类,它可以代替QLatin1String类。QLatin1String类因为兼容性的原因无法被改变。QLatin1Literal 存储了它的长度,因此当QStringBuilder 计算最后的字符串的大小时,可以节省时间。
另外还通过内联潜入和减少引用次数(通过QStringBuilder 创立的QString,引用数是1,而QString::append()需要额外的测试)提高效率。
有三种方式,可以使用这种改善string构造效率的方法。
直接方式就是包含QStringBuilder ,当你在任何地方用到它的时候。然后当连接字符串的时候,用'%'代替'+'。
#include <QStringBuilder>
QString hello("hello");
QStringRef el(&hello, 2, 3);
QLatin1String world("world");
QString message = hello % el % world % QChar('!');
另外一种:全局的方式,定义一个宏定义。然后用用'%'代替'+'
//使用快速连接
#define QT_USE_FAST_CONCATENATION
and use '%' instead of '+' for string concatenation everywhere.
第三种方式:最方便,但是并不是所有代码都会兼容。就是包含两个宏定义即可。
#define QT_USE_FAST_CONCATENATION
#define QT_USE_FAST_OPERATOR_PLUS
然后所有的'+'就会被当做QStringBuilder 的'%'执行。
但在Qt 4.8、5.0,这个宏已被新的QT_USE_QSTRINGBUILDER宏所替代。(只需要这一个宏,就可以自动把'+'当做'%'执行)