当前位置:网站首页>Detailed explanation of QT qstring

Detailed explanation of QT qstring

2022-07-28 16:25:00 icewst

Some descriptions :

QString Class is Qt Class used to represent strings in , Realize in QtCore In the shared library .QString Class has the following characteristics in implementation .

  • String adoption Unicode Internal encoding , Words that can express most languages in the world ; The string is stored with a reference count , When one QString Object is copied as another
  • QString Object time , They actually point to the same storage space , Just add a reference count ; use “ Copy on demand ” Technology , When pointing to multiple of the same storage space
  • QString When one of the objects is to be modified , Will actually copy a new string and modify it .
1. establish
QString str1 = "Welcome";

// The following is wrong !!! After initialization , To add !
QString str2 = "Welcome"+" OK";

// correct 
QString str3="Welcome";
str3=str3+"Hello";
2. Connection splicing
QString abc="sss";
abc="xxx"+abc+"123"+abc;
3. Compare
    QString str="hello" ;
    if(str =="hello")
    {
    
        qDebug("xiangtong");
    }

The result is xiangtong

 // return int Type values , be equal to 0 It means the same , It's not equal to 0 Different 
    //Qt::CaseInsensitive  This type , Indicates case insensitive  Qt::CaseSensitive  Indicates case sensitivity 
    QString str="Hello" ;
    if(str.compare("hello",Qt::CaseInsensitive)==0)
    {
    
        qDebug("xiangtong");
    }

The result is xiangtong

4. Is it an empty string
QString str;
    if(str==NULL){
    
        qDebug("kong");
    }
    
    if(str==""){
    
        qDebug("kong");
    }

QString str1="";
    if(str1==""){
    
        qDebug("kong");
    }
    

The results of all three are kong

5. Convert case
    QString str="Hello" ;
    str=str.toLower();
    qDebug(str.toLatin1()); // The output here is : hello

    str=str.toUpper();
    qDebug(str.toLatin1()); // The output here is : HELLO
6. To obtain the length of the
    QString str="Hello" ;
    int len=str.length();
    qDebug("%d",len); // The output here is :5
7. Remove space
    //trimmed Only remove the spaces at the beginning and end 
    QString str=" He llo " ;
    str=str.trimmed();
    qDebug(str.toLatin1()); // Output results :He llo


    // by this means , To remove all spaces 
    QString str1=" He llo " ;
    str1=str1.remove(QRegExp("\\s"));
    qDebug(str1.toLatin1());// Output results :Hello
8. Get substring
    //mid The first parameter of  position  Is the starting position of the substring 
    //mid The second parameter of  n  Is the number of strings 
    //  If n  by  -1, It means to the end of the original string .
    QString str="Hello" ;
    QString str1=str.mid(0,2);
    qDebug(str1.toLatin1());// The output is :He
9. Find string

    // Default comparison , Case sensitive 
    QString str="Hello" ;
    if(str.contains("hel")){
    
         qDebug("yes");
    }else{
    
         qDebug("no");
    }
    // The output is :no
  
    // Case insensitive comparison 
    QString str="Hello" ;
    if(str.contains("hel",Qt::CaseInsensitive)){
    
         qDebug("yes"); 
    }
    // The output is :yes CaseInsensitive Indicates case insensitive 
10. Replace string
    QString str="Hello" ;

    str=str.replace("llo","xxx");
    qDebug(str.toLatin1()); // The output is :Hexxx
11. and char* Conversion between
    //char* turn QString
    char charArry[] = "hello";
    char* ch = charArry;
    QString str = Qstring::fromUtf8(ch); 

    //QString turn char*, First to string, Avoid miscoding in Chinese 
    QString filename="he Chinese llo";
    std::string str = filename.toStdString();
    const char* ch = str.c_str();
    qDebug(ch);

    //QString turn char*
    QString  str= "he Chinese llo";
    char*  ch ;
    QByteArray ba = str.toUtf8();// This sentence avoids Chinese garbled , This sentence and the following sentence cannot be written together 
    ch=ba.data();
    qDebug(ch);
12. Conversion between string and numeric value
    QString str="20";
    
    int strInt=str.toInt();
    int num=100;
    
    QString  str= QString::number(num);

matters needing attention :

  • When comparing Chinese
  • Chinese do and char* When switching between
原网站

版权声明
本文为[icewst]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207281510091059.html