Changing QStringList with number to another QStringList



  • Hi,
    I have a QStringList which has
    winter1.jpg
    winter2.jpg
    winter3.jpg
    ...
    winter999.jpg
    I need to changed this to another QStringList which has
    winter001.jpg
    winter002.jpg
    winter003.jpg
    ...
    winter999.jpg
    How can I change it in Qt?



  • I'll go with a foreach and a regular expression.
    Or with a lambda function and a std::transform.



  • @samdol

    Really depends on how universal your transformation has to be.

    In the case you describe, I would probably do something like this:

    QString addZeros(QString s, int size = 3){
         while(s.size() < size)
             s.insert(0,'0');
         return s;
    }
    ....
    
    for(QString sEntry : myStringList){
        QString replaceString;
        for(int i = 0; i< sEntry.size(); 
             if(sEntry.at(i) >= QChar('0')  &&  sEntry.at(i) <= QChar('9'))
                replaceString.append(sEntry.at(i));
        }
        sEntry.replace(replaceString, addZeros(replaceString));
    }
    


  • @J.Hilk
    Thank you for your answer. It worked like a charm.


Log in to reply
 

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