Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Changing QStringList with number to another QStringList

Changing QStringList with number to another QStringList

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 979 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samdol
    wrote on last edited by samdol
    #1

    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?

    J.HilkJ 1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

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

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      2
      • S samdol

        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?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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));
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        S 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @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));
          }
          
          S Offline
          S Offline
          samdol
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved