QString+char split



  • Hello All

    I have string text for example "98745.95", "98745,95", "98745/95" and i want it to divide into two parts:
    QString a1="98745.95" //or "98745/95" or "98745,95"
    char separators[] ={' . ' , ' , ' , ' / '};
    QString parts = a1.split(separators); // Here it show error;
    int n1=parts[0].length(); // Here it must be length of "98745"
    int n2=parts[1].length(); // Here it must be length of "95"
    Question is how could i implement this with char variable?

    P.S.
    This code works, but i want to try with code above
    QString a1="98745.95";
    QStringList strList = a1.split("."); // Here i show separator exactly
    QString parts[2];
    parts[0]=strList.at(0);
    parts[1]=strList.at(1);
    qDebug() << parts[0] << "-" << parts[1];

    Regards Jasur,
    from Tashkent


  • Qt Champions 2017

    Hi @Jasur ,

    please try the following:

    #include <QCoreApplication>
    #include <QDebug>
    #include <QString>
    #include <QRegularExpression>
    
    int main(int argc, char *argv[])
    {
    	QString a1 = "98745.95"; //or "98745/95" or "98745,95"
    	QStringList parts = a1.split(QRegularExpression("[\\.,/]"));
    
    	qDebug() << parts;
    }
    

    Also take care after split, if you access parts[1] or parts.at(1), you have to check that parts.size() > 1 before or you get a crash if the string could not be split ;)



  • @aha_1980 Thank you very much it works. This is what i wanted.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.