2.1 QString

字符串操作

    QString str1="Welcome ";
    QString str2="to ";
    str1.append(str2);
    str1.append(" you");
    ui->textEdit->append(str1);
    
    str1.clear();
    str1.sprintf("%s","Welcome");
    ui->textEdit->append(str1);
    str1.sprintf("%s %s","Welcome","to you");
    ui->textEdit->append(str1);
    
    str1=QString("%1 was born in %2").arg("John").arg(1998);
    ui->textEdit->append(str1);

字符串查询

str1="Welcome to you";
//查询字符串是否以某个字符串开头,第二个参数指定大小写是否敏感
bool f1=str1.startsWith("Welcome",Qt::CaseSensitive);
bool f2=str1.startsWith("welcome",Qt::CaseSensitive);
//查询字符串是否以某个字符串结尾
bool f3=str1.endsWith("to you",Qt::CaseInsensitive);
//查询字符串是否包含某个字符串
bool f4=str1.contains("welcome",Qt::CaseInsensitive);
QString line=QString("f1=%1 f2=%2 f3=%3 f4=%4").arg(f1).arg(f2).arg(f3).arg(f4);
ui->textEdit->append(line);

 

字符串转换

    //字符串转换
    QString str="125";
    bool ok;
    //第一个参数表示转换是否成功
    int hex=str.toInt(&ok,16);
    int dec=str.toInt(&ok,10);
    line=QString("125 toInt: \n\thex=%1 dec=%2").arg(hex).arg(dec);
    ui->textEdit->append(line);
    QByteArray ba=str.toLatin1();
    qDebug()<<"ba:"<<ba;
    ba.append(" Hello, World");
    qDebug()<<"ba:"<<ba;

 

 

 

posted @ 2020-02-13 11:19  啸傲风月  阅读(222)  评论(0编辑  收藏  举报