Qt QString详解

1.QString 常用方法

    //append 在后面添加字符串,prepend在前面添加字符串
    QString s1 = "张";
    QString s2 = "三";
    QString s3 = s1;
    s1.append(s2);      //s1 = "张三"
    s3.prepend(s2);     //s3 = "三张"

    //toUpper,toLower转换大小写
    QString s4 = "Hello,World";
    QString s5,s6;
    s5 = s4.toUpper();      //HELLO,WORLD
    s6 = s4.toLower();      //hello,world

    //字符串的个数
    int nCount = s4.count();    //nCount = 11
    nCount = s4.size();         //nCount = 11
    nCount = s4.length();       //nCount = 11

    //trimmed去掉字符串首尾的空格,simplified不仅去掉首尾的空格,中间连续的空格也用一个空格代替
    QString s7 = "  QString S7    Are you OK  ? ";
    QString s8,s9;
    s8 = s7.trimmed();    //s8 = "QString S7    Are you OK  ?"
    s9 = s7.simplified(); //s9 = "QString S7 Are you OK ?"

    //indexOf查找字符串首次出现的位置,没找到返回-1,lastIndexOf最后一次出现的位置
    QString s10 = "Are you eOK";
    nCount = s10.indexOf("you");   //4
    nCount = s10.lastIndexOf("e"); //8

    //判断是否为空,只有未赋值的字符串isNull返回true
    QString s11,s12 = "";
    bool bRet = s11.isNull(); //true
    bRet = s11.isEmpty();     //true
    bRet = s12.isNull();      //false
    bRet = s12.isEmpty();     //true

    //判断是否包含字符串
    s10 = "Are you eOK";
    bRet = s10.contains("you",Qt::CaseInsensitive); //不区分大小写 true
    bRet = s10.contains("YOu",Qt::CaseSensitive);   //区分大小写 false

    //判断是否以字符串结尾,字符串开始
    s10 = "Are you eOK";
    bRet = s10.endsWith("eOK",Qt::CaseInsensitive); //不区分大小写 true
    bRet = s10.endsWith("EoK",Qt::CaseInsensitive); //区分大小写 false
    bRet = s10.startsWith("a");                     //缺省为不区分大小写true

    //从左边取多少个字符,从右边取多少个字符
    QString x = "Pineapple";
    QString y1 = x.left(4);       // y == "Pine"
    QString y2 = x.right(5);      // y == "apple"

    //从字符串中提取 以第一个参数作为分隔符 ,从第二个参数 到 第三个参数的字符串。
    QString str;
    QString csv = "forename,middlename,surname,phone";
    QString path = "/usr/local/bin/myapp";                          // First field is empty

    str = csv.section(',', 2, 2);                                   // str == "surname"
    str = path.section('/', 3, 4);                                  // str == "bin/myapp"
    str = path.section('/', 3, 3, QString::SectionSkipEmpty);       // str == "myapp"

    //分割字符串
    QStringList list1 = path.split("/");

    //字符串 -> 数字
    int number = QString("23").toInt();
    //数字 -> 字符串
    QString strNum = QString::number(23);

 

要使用Qt中的QFile和QTextStream类将QString写入文件,可以按照以下步骤进行操作: 1. 首先,包含必要的头文件,如QFile、QTextStream和QTextCodec。例如: ``` #include <QFile> #include <QTextStream> #include <QTextCodec> ``` 2. 创建一个QFile对象并指定文件路径。例如: ``` QFile file("D:/Qt.txt"); ``` 3. 打开文件以进行写入,使用QIODevice::ReadWrite和QIODevice::Text参数。例如: ``` file.open(QIODevice::ReadWrite | QIODevice::Text); ``` 4. 将要写入的QString内容转换为QByteArray格式,以便写入文件。可以使用QString的toUtf8()方法进行转换。例如: ``` QString content = "写入文件的内容"; QByteArray str = content.toUtf8(); ``` 5. 使用QTextStream对象将转换后的内容写入文件。例如: ``` QTextStream stream(&file); stream << str; ``` 6. 关闭文件。例如: ``` file.close(); ``` 这样,你就成功将QString写入文件了。如果文件已经存在,可以通过判断文件是否存在来避免覆盖原文件。如果文件已经存在,可以选择是否进行覆盖操作。例如: ``` if (file.exists()) { QMessageBox::warning(this, "创建文件", "文件已经存在!"); } else { // 文件不存在,进行写入操作 // ... } ``` 如果你想修改已经存在的文件,可以先打开文件并读取其内容,然后再进行修改并保存。你可以使用类似以下的代码: ``` // 打开文件并读取内容 QFile file("D:/Qt.txt"); file.open(QIODevice::ReadOnly | QIODevice::Text); QTextStream ts(&file); QString str_get = ts.readAll(); file.close(); // 进行修改并保存 file.open(QIODevice::ReadWrite | QIODevice::Text); QByteArray strb = str_get.toUtf8(); file.write(strb); file.close(); ``` 请注意,这只是一个示例,你可以根据实际需求进行修改和适应。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Qt文件操作详解(创建、写入、删除、INI、XML文件等)](https://blog.csdn.net/qq_40194498/article/details/79726388)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灬Sunnnnn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值