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. Why is QString::remove not working as expected?
Forum Updated to NodeBB v4.3 + New Features

Why is QString::remove not working as expected?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 710 Views 2 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.
  • H Offline
    H Offline
    HFT_developer
    wrote on last edited by
    #1

    Hi, I am trying to remove certain chars form a QString however the remove function is doing something funny:

        // remove all instances of power, wheather negative positive
        for (int i{};i<_numerator.size();++i){
            if (_numerator[i] == coeff){
                if (_numerator[i+2] == '-'){
                    _numerator.remove(i+1, 3);
                }
                else{
                    _numerator.remove(i+1, 2);
                }
            }
        }
    

    For given a string of "15x^-2+10x^-3" I want to modify it to "15x+10x". Can someone help me as to why this doesn't work?

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums

      It is a classic mistake.
      when you loop over a container using its size and in that loop, remove elements, fun things are bound to happen.

      Try to loop it reversed and see if that cures it.

      If not, you need to copy on the fly the parts you want and then skip the unwanted ones.

      1 Reply Last reply
      0
      • H HFT_developer

        Hi, I am trying to remove certain chars form a QString however the remove function is doing something funny:

            // remove all instances of power, wheather negative positive
            for (int i{};i<_numerator.size();++i){
                if (_numerator[i] == coeff){
                    if (_numerator[i+2] == '-'){
                        _numerator.remove(i+1, 3);
                    }
                    else{
                        _numerator.remove(i+1, 2);
                    }
                }
            }
        

        For given a string of "15x^-2+10x^-3" I want to modify it to "15x+10x". Can someone help me as to why this doesn't work?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @HFT_developer
        Hello and welcome.

        Assuming coeff is the ^ character you know that is at _numerator[i]. Yet you look for any - symbol at i+2 when it would come immediately after the ^ which would be i+1. And you only start deleting from i+1 which is after the ^ so that will be retained, which you do not want.

        Untested, but don't you want:

                    if (_numerator[i+1] == '-'){
                        _numerator.remove(i, 3);
                    }
                    else{
                        _numerator.remove(i, 2);
                    }
        

        ?

        Note that you are making assumptions here like (a) once you meet a ^ or ^- there will be at least one character after it and (b) only one character after it, e.g. 15x^-20+10x^333 would not be replaced correctly at either ^... sequence.

        H 1 Reply Last reply
        0
        • JonBJ JonB

          @HFT_developer
          Hello and welcome.

          Assuming coeff is the ^ character you know that is at _numerator[i]. Yet you look for any - symbol at i+2 when it would come immediately after the ^ which would be i+1. And you only start deleting from i+1 which is after the ^ so that will be retained, which you do not want.

          Untested, but don't you want:

                      if (_numerator[i+1] == '-'){
                          _numerator.remove(i, 3);
                      }
                      else{
                          _numerator.remove(i, 2);
                      }
          

          ?

          Note that you are making assumptions here like (a) once you meet a ^ or ^- there will be at least one character after it and (b) only one character after it, e.g. 15x^-20+10x^333 would not be replaced correctly at either ^... sequence.

          H Offline
          H Offline
          HFT_developer
          wrote on last edited by HFT_developer
          #4

          @JonB yeah your right. Could you recommend a better algorithm?

          JonBJ 1 Reply Last reply
          0
          • H HFT_developer

            @JonB yeah your right. Could you recommend a better algorithm?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @HFT_developer
            If I were writing this I would probably use the overload QString &QString::remove(const QRegularExpression &re) which uses a regular expression to do all of the removals in one call:

            Removes every occurrence of the regular expression re in the string

            There is a bit of an issue specifying the ^ character because that can mean "beginning of string` in a regular expression. Again untested, but how does this behave:

            _numerator.remove(QRegularExpression("\\^-?\\d+"));
            

            ? That is intended to remove (a) a literal ^ (b) optionally immediately followed by a single - and (c) immediately followed by one or more digits.

            H 1 Reply Last reply
            2
            • JonBJ JonB

              @HFT_developer
              If I were writing this I would probably use the overload QString &QString::remove(const QRegularExpression &re) which uses a regular expression to do all of the removals in one call:

              Removes every occurrence of the regular expression re in the string

              There is a bit of an issue specifying the ^ character because that can mean "beginning of string` in a regular expression. Again untested, but how does this behave:

              _numerator.remove(QRegularExpression("\\^-?\\d+"));
              

              ? That is intended to remove (a) a literal ^ (b) optionally immediately followed by a single - and (c) immediately followed by one or more digits.

              H Offline
              H Offline
              HFT_developer
              wrote on last edited by
              #6

              @JonB I checked and that works well. I have another question about converting a list to another. In STL you have std::transform to do that. What is the equivalent for this in qt?

              For example, I want to convert a QList<QString> to QList<double>, in this case I can guarantee that is will contain something like {"15.5", "19.9", "25.5"} there wont be any non numbers. Right now I am using a simple loop:

                  QList<QString> _splitter = data.split(coeff);
                  QList<double> ret;
              
                  // transform into double from QString
                  for (int i{};i<_splitter.size();++i){
                      ret.push_back(_splitter[i].toDouble());
                  }
              

              what are your thoughts on this?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                This is valid. You could also use std::transform.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

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