QString详解

QString存储16位Qchar(Unicode)字符串
QString使用隐式共享(copy-on-write)来提高性能。

什么是Unicode?

unicode是一种国际标准,支持当今使用的大多数操作系统,他是US-ASCII和Latin-1的超集(与子集相同字符编码相同)

QString字符串的创建

#include "mainwindow.h"

#include 

int main(int argc, char* argv[])
{
    //    QApplication a(argc, argv);

    QString b;
    QString c { "lave" }; //大括号,括住代表初始化
    c = "cest";
    c = ("ce");
    //如果是std::string字符串,需要使用c_str();转换
    std::string c1 = "c1";
    QString c2 = c1.c_str();
    qDebug() << "C2=" << c2;

    std::string s1 = "s1";
    QString s2 = QString::fromLatin1(s1.data(), s1.size());
    //    QString s2 = QString::fromStdString(s1);
    qDebug() << "s1.data=" << s1.data() << " s1.size=" << s1.size(); //获取s1的数据和大小

    qDebug() << "s2=" << s2;

    char c3[] = "a array";
    QString s3 = QString(c3);
    qDebug() << "s3=" << s3;

    b.append("class"); //后面添加
    b.prepend("我"); //前面添加
    qDebug() << "c=" << c;
    qDebug() << "b=" << b;
    qDebug() << "this is string has " << b.count() << "characters"; // b.count()获取字符长度,中文占一个
    qDebug() << b.toUpper(); //转为大写
    qDebug() << b.toLower(); //小写
    qDebug() << b[0]; //可以修改
    qDebug() << b.at(1); //不可修改
    qDebug() << b[12]; //超出,报错

    //    return a.exec();
}

构建字符串

 //构造字符串
    QString s1 = "这是第%1个例子";
    int n = 7;
    qDebug() << s1.arg(n); //&1代表占位符
    QString s2 = "We have %1 lemons and %2 oranges";
    double ln = 1.2;
    double on = 3;
    qDebug() << s2.arg(ln).arg(on);

子字符串

  QString s1 = "The Right Left";
    qDebug() << s1.right(5); //从右边取5个
    qDebug() << s1.left(4); //从左边取4个

遍历字符串

需要添加命名空间 

using namespace Qt;
#include "mainwindow.h"

#include 
#include 

using namespace Qt;

int main(int argc, char* argv[])
{
    QString s1 = "我有一个梦想!";
    QTextStream out(stdout); //标准输出
    out.setEncoding(QStringConverter::System); //设置编码格式,为系统编码
    //方式一:
    //    for (QChar qc : s1) { //遍历 范围for
    //        out << qc << "";
    //    }
    //方式二:
    //    for (QChar* it = s1.begin(); it != s1.end(); it++) {
    //        out << *it << "";
    //    }
    //方式三:
    for (int i = 0; i < s1.size(); i++) {
        out << s1.at(i) << ""; //at比[]快,因为at只读
    }
    out << endl;

    return 0;
}

字符串比较

QString::compare返回整型:
        0表示相等
        负数表示小于
        正数表示大于

#include "mainwindow.h"

#include 
#include 
using namespace Qt;
#define STR 0 //定义宏

int main(int argc, char* argv[])
{
    const int STR_EQUAL = STR;

    QString s1 = { "Rain" };
    QString s2 = { "rain" };
    QString s3 = { "rain\n" };
    if (QString::compare(s2, s3) == 0) {
        qDebug() << "s2和s3相等";
    } else {
        qDebug() << "s2和s3不相等";
    }
    if (QString::compare(s1, s2, Qt::CaseInsensitive) == STR_EQUAL) { //大小写不敏感
        qDebug() << "s2和s3相等";
    } else {
        qDebug() << "s2和s3不相等";
    }
    
    s3.chop(1); //从尾部删除1个字符
    
    
    return 0;
}

字符类型

字符分为:数字,字母,空白字符,和标点符号
QChar 有 isDigit、isleter、 isSpaec 、isFunt函数

#include "mainwindow.h"

#include 
#include 
using namespace Qt;
#define STR 0 //定义宏

int main(int argc, char* argv[])
{
    const int STR_EQUAL = STR;

    QString s1 = { "7 while ,3 red roces." };
    int digits = 0, letters = 0, spaces = 0, puncts = 0; //赋值,不可以使用,分开,必须全部
    for (QChar ch : s1) {
        if (ch.isDigit()) {
            digits++;
        } else if (ch.isLetter()) {
            letters++;
        } else if (ch.isSpace()) {
            spaces++;
        } else if (ch.isPunct()) {
            puncts++;
        }
    }
    qDebug() << QString("There are %1 characters").arg(s1.count());
    qDebug() << QString("There are %1 digits").arg(digits);
    qDebug() << QString("There are %1 letters").arg(letters);
    qDebug() << QString("There are %1 spaces").arg(spaces);
    qDebug() << QString("There are %1 puncts").arg(puncts);

    return 0;
}

字符串类型转换

#include "mainwindow.h"

#include 
#include 
using namespace Qt;
#define STR 0 //定义宏

int main(int argc, char* argv[])
{
    const int STR_EQUAL = STR;

    QString s1 = { "12" };
    QString s2 = { "15" };
    QString s3, s4;
    qDebug() << s1.toInt() + s2.toInt(); //转为整型
    int n1 = 30;
    int n2 = 40;
    qDebug() << s3.setNum(n1) + s4.setNum(n2); //数字变为字符串
    QString s5 = "This is ";
    s5.remove(2, 1); //在第2个位置删除1个 "ths is "
    s5.replace(5, 1, "c"); // "ths ic "
    qDebug() << s5.toLower();
    qDebug() << s5.toUpper();
    QString allText = "<\"一级标题\">";
    allText.toHtmlEscaped(); //将纯文本字符串转为具有html元字符的HTML字符串
    qDebug() << allText;

    QString field1 { "name" };
    qDebug() << field1.rightJustified(10, '.') << "Robert"; //右对齐
    return 0;
}

你可能感兴趣的:(QT,ui)