QString与QStringList
对于我而言,这两者用的频率真的非常高了。
一、QString
根据api顺序,把自己现在的经验写下来。
函数名 | 意义 |
---|---|
QString & append(const QString & str); | 追加一个串到尾部,有多个重载 |
QString arg(const QString & a, int fieldWidth = 0, QChar fillChar = QLatin1Char( ‘ ‘ )) const; | 使用a替换%1 最少占用fieldWidth字符,不足用fillChar替换 fieldWidth为正,右对齐。反之,左对齐 |
QString QString::arg(const QString & a1, const QString & a2) const; |
QString str;
str = “%1 %2”;
str.arg(“%1f”, “Hello”); // returns “%1f Hello”
str.arg(“%1f”).arg(“Hello”); // returns “Hellof %2” | |
| int QString::compare(const QString & s1, const QString & s2, Qt::CaseSensitivity cs = Qt::CaseSensitive);
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 | 比较两个串 |
| bool QString::contains(const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
QString str = “Peter Pan”;
str.contains(“peter”, Qt::CaseInsensitive); // returns true | 是否包含str |
| int QString::count(const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; | 包含str多少次 |
| QChar QString::data(); | 将QString转成QChar字符串 |
| bool QString::startsWith(const QString & s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; | 是否以s开始 |
| bool QString::endsWith(const QString & s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; | 是否以s结束 |
| QString & QString::fill(QChar ch, int size = -1); | 使用连续size个ch填充 |
| [static] QString QString::fromLatin1(const char str, int size = -1); | 从char 转入 |
| [static] QString QString::fromLocal8Bit(const char str, int size = -1); | 从8字节长度char 转入 |
| [static] QString QString::fromRawData(const QChar unicode, int size); | 从unicode转入 |
| [static] QString QString::fromStdString(const std::string & str); | 从标准string转入 |
| [static] QString QString::fromStdU16String(const std::u16string & str); | 从标准16字长的string转入 |
| [static] QString QString::fromStdWString(const std::wstring & str); | 从标准宽字节转入 |
| [static] QString QString::fromUtf8(const char str, int size = -1); | 从utf8字串转入 |
| [static] QString QString::fromUtf16(const ushort unicode, int size = -1); | 从unicode字串转入 |
| [static] QString QString::fromWCharArray(const wchar_t * string, int size = -1); | 从宽字串转入 |
| int QString::indexOf(const QString & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; | 从from位置开始 str出现的位置 没有返回-1 |
| QString & QString::insert(int position, const QString & str); | 从position位置插入str |
| bool QString::isEmpty() const;
QString().isEmpty(); // returns true
QString(“”).isEmpty(); // returns true
QString(“x”).isEmpty(); // returns false
QString(“abc”).isEmpty(); // returns false | |
| bool QString::isNull() const;
QString().isNull(); // returns true
QString(“”).isNull(); // returns false
QString(“abc”).isNull(); // returns false | |
| QString QString::left(int n) const; | 保留左边起n个 |
| QString QString::right(int n) const; | 保留右边起n个 |
| QString QString::mid(int position, int n = -1) const; | 保留position位置起,连续n个 |
| [static] QString QString::number(long n, int base = 10); | 将n转成串 |
| | |
| QString & QString::prepend(const QString & str);
QString x = “ship”;
QString y = “air”;
x.prepend(y);
// x == “airship” | 将str做为前缀 |
| QString & QString::remove(int position, int n); | 从Position删除连续n个 |
| QString QString::repeated(int times) const; | 串重复times次 |
| QString & QString::replace(int position, int n, const QString & after);
//不止下面两种
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 QString::section(QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
QString str;
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” | 以sep切开,也可以以串切开 |
| QString QString::simplified() const;
QString str = “ lots\t of\nwhitespace\r\n “;
str = str.simplified();
// str == “lots of whitespace”; | 去除整串的占位符 |
| QStringList QString::split(const QString & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
SplitBehavior:
QString::KeepEmptyParts 0 If a field is empty, keep it in the result.
QString::SkipEmptyParts 1 If a field is empty, don’t include it in the result.
str = “This time, a normal English sentence.”;
list = str.split(QRegExp(“\W+”), QString::SkipEmptyParts);
// list: [ “This”, “time”, “a”, “normal”, “English”, “sentence” ] | 切割 |
| QByteArray QString::toLatin1() const; | |
| QByteArray QString::toLocal8Bit() const; | |
| std::string QString::toStdString() const | |
| QString QString::toLower() const; | 转成小写 |
| QString QString::toUpper() const; | 转成大写 |
| QString QString::trimmed() const; | 去掉首尾占位 |
| void QString::truncate(int position);
QString str = “Vladivostok”;
str.truncate(4);
// str == “Vlad” | 与left有点类似 保留前position个 |
| const QChar QString::unicode() const; | 字面意思 |
| const ushort QString::utf16() const; | |
1 | [static] QString QString::number(double n, char format = 'g', int precision = 6); |
1 | QString与string的转换,经实验,string与QString使用API互相转换,中文会乱码 |
二、QStringList
其实就是QString的数组
函数名 | 意义 |
---|---|
bool QStringList::contains(const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; | 是否包含str |
QStringList QStringList::filter(const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; |
QStringList list;
list << “Bill Murray” << “John Doe” << “Bill Clinton”;
QStringList result;
result = list.filter(“Bill”);
// result: [“Bill Murray”, “Bill Clinton”] | 过虑出包含str的串 |
| int QStringList::indexOf(const QRegExp & rx, int from = 0) const; | 匹配 |
| QStringList & QStringList::replaceInStrings(const QString & before, const QString & after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
QStringList list;
list << “alpha” << “beta” << “gamma” << “epsilon”;
list.replaceInStrings(“a”, “o”);
// list == [“olpho”, “beto”, “gommo”, “epsilon”] | 替换 |
| void QStringList::sort(Qt::CaseSensitivity cs = Qt::CaseSensitive); | 排序 |
本文标题:QString与QStringList
文章作者:whppmy
发布时间:2019-01-26
最后更新:2019-01-26
原始链接:http://bugnull.com/QT/qstring与qstringlist/
版权声明:个人记录,没有获取同意时,禁止转载!!