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. Can anybody help me for find next index of particular string ?
Forum Updated to NodeBB v4.3 + New Features

Can anybody help me for find next index of particular string ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 264 Views
  • 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 16 Dec 2022, 07:13 last edited by
    #1

    i have one string like below:

    QString str = Ddhh (copy)(1).txt;

    i have to each time increase the 1 by 1. means if its 1 then i have to make it 2. if its 2 then i have tom make it 3.

    can anybody suggest me way to achieve this with example ?

    J 1 Reply Last reply 16 Dec 2022, 07:40
    0
    • Q Qt embedded developer
      16 Dec 2022, 07:13

      i have one string like below:

      QString str = Ddhh (copy)(1).txt;

      i have to each time increase the 1 by 1. means if its 1 then i have to make it 2. if its 2 then i have tom make it 3.

      can anybody suggest me way to achieve this with example ?

      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 16 Dec 2022, 07:40 last edited by
      #2

      @Qt-embedded-developer
      because I'm feeling festive!
      Here's a very crude and unoptimised way:

      QString s{"Ddhh (copy)(1).txt"};
      
          for(int i {10}, j{50}; i < j; i++)
          {
              auto indexStart = s.lastIndexOf(QRegularExpression("\\(.+?\\)"));
              auto indexEnd = s.indexOf(QRegularExpression("\\)"), indexStart+1);
      
              QString left = s.left(indexStart +1);
              QString right = s.right(s.size() - indexEnd);
      
              int copyCount = s.midRef(indexStart +1, indexEnd - indexStart -1).toInt();
      
              s = left + QString::number(++copyCount) + right;
              qDebug() << s;
          }
      

      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.

      Q 1 Reply Last reply 16 Dec 2022, 09:10
      2
      • J J.Hilk
        16 Dec 2022, 07:40

        @Qt-embedded-developer
        because I'm feeling festive!
        Here's a very crude and unoptimised way:

        QString s{"Ddhh (copy)(1).txt"};
        
            for(int i {10}, j{50}; i < j; i++)
            {
                auto indexStart = s.lastIndexOf(QRegularExpression("\\(.+?\\)"));
                auto indexEnd = s.indexOf(QRegularExpression("\\)"), indexStart+1);
        
                QString left = s.left(indexStart +1);
                QString right = s.right(s.size() - indexEnd);
        
                int copyCount = s.midRef(indexStart +1, indexEnd - indexStart -1).toInt();
        
                s = left + QString::number(++copyCount) + right;
                qDebug() << s;
            }
        
        Q Offline
        Q Offline
        Qt embedded developer
        wrote on 16 Dec 2022, 09:10 last edited by
        #3

        @J-Hilk Dear J-hilk. your code helped me but may i know the reason why you have taken for loop ?

        J 1 Reply Last reply 16 Dec 2022, 09:15
        0
        • Q Qt embedded developer
          16 Dec 2022, 09:10

          @J-Hilk Dear J-hilk. your code helped me but may i know the reason why you have taken for loop ?

          J Online
          J Online
          J.Hilk
          Moderators
          wrote on 16 Dec 2022, 09:15 last edited by
          #4

          @Qt-embedded-developer as a, also very crude, Unit-Test. See if it works multiple times, and for multiple ranges of numbers


          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.

          1 Reply Last reply
          0

          3/4

          16 Dec 2022, 09:10

          • Login

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