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. remove char from qstring
Forum Updated to NodeBB v4.3 + New Features

remove char from qstring

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 6.9k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    So you want to remove all zeroes before the first one ?
    Is it specifically that or are there other patterns ?

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

    S 1 Reply Last reply
    2
    • SGaistS SGaist

      Hi,

      So you want to remove all zeroes before the first one ?
      Is it specifically that or are there other patterns ?

      S Offline
      S Offline
      s002wjh
      wrote on last edited by s002wjh
      #3

      @SGaist yes I would like remove all prefix zeros. so any 0 before 1 or a number/char is removed

      JonBJ jsulmJ 2 Replies Last reply
      0
      • S s002wjh

        @SGaist yes I would like remove all prefix zeros. so any 0 before 1 or a number/char is removed

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

        @s002wjh
        If you really mean these are all numbers which might have leading zeroes you'd like to remove, would it best to convert them from strings to integers, and then write that back to string (if you need to)? Is that what you mean?

        Otherwise it can be done by regular expression substitution/replacement.

        And btw I'm thinking your

        so any 0 before 1 or a number/char is removed

        means any 0 before any other numeric character/digit, 1--9. As opposed to any character, so if you encountered 0A... you would not want that 0 removed, right?

        1 Reply Last reply
        3
        • GabrielRRG Offline
          GabrielRRG Offline
          GabrielRR
          wrote on last edited by
          #5

          You can use a simple javascript function like:

          function myFunction() {
              var str = "0101";
              var n = str.indexOf("1");
              var subString= str.substring(n);
              console.log("Result", subString)
          }
          

          The output should be 101.

          Lic-Ing. Jose Gabriel Lopez Villalobos
          Embedded Software Engineer
          RidgeRun Engineering Ltd.
          www.ridgerun.com
          Email: gabriel.lopez@ridgerun.com

          JonBJ 1 Reply Last reply
          0
          • GabrielRRG GabrielRR

            You can use a simple javascript function like:

            function myFunction() {
                var str = "0101";
                var n = str.indexOf("1");
                var subString= str.substring(n);
                console.log("Result", subString)
            }
            

            The output should be 101.

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

            @GabrielRR
            Why would you use JavaScript and how would you call it if you're in Qt (presumably C++) with a QString?

            GabrielRRG 1 Reply Last reply
            0
            • JonBJ JonB

              @GabrielRR
              Why would you use JavaScript and how would you call it if you're in Qt (presumably C++) with a QString?

              GabrielRRG Offline
              GabrielRRG Offline
              GabrielRR
              wrote on last edited by
              #7

              @JonB my bad, I though this was on qml and not c++

              Lic-Ing. Jose Gabriel Lopez Villalobos
              Embedded Software Engineer
              RidgeRun Engineering Ltd.
              www.ridgerun.com
              Email: gabriel.lopez@ridgerun.com

              1 Reply Last reply
              0
              • S s002wjh

                @SGaist yes I would like remove all prefix zeros. so any 0 before 1 or a number/char is removed

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by aha_1980
                #8

                @s002wjh You can use https://doc.qt.io/qt-5/qstring.html#remove-5 with a regular expression like (didn't test):

                str.remove(QRegularExpression("^0+"));
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                aha_1980A JonBJ 2 Replies Last reply
                2
                • jsulmJ jsulm

                  @s002wjh You can use https://doc.qt.io/qt-5/qstring.html#remove-5 with a regular expression like (didn't test):

                  str.remove(QRegularExpression("^0+"));
                  
                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @jsulm Fixed it for you :)

                  Qt has to stay free or it will die.

                  jsulmJ 1 Reply Last reply
                  0
                  • aha_1980A aha_1980

                    @jsulm Fixed it for you :)

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @aha_1980 Thanks! :-)

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @s002wjh You can use https://doc.qt.io/qt-5/qstring.html#remove-5 with a regular expression like (didn't test):

                      str.remove(QRegularExpression("^0+"));
                      
                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #11

                      @jsulm

                      str.remove(QRegularExpression("^0+"));

                      How come you get away with this, when I am trying to stick with OP's stated requirement:

                      so any 0 before 1 or a number/char is removed
                      

                      means any 0 before any other numeric character/digit, 1--9. As opposed to any character, so if you encountered 0A... you would not want that 0 removed, right?

                      ? :)

                      jsulmJ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @jsulm

                        str.remove(QRegularExpression("^0+"));

                        How come you get away with this, when I am trying to stick with OP's stated requirement:

                        so any 0 before 1 or a number/char is removed
                        

                        means any 0 before any other numeric character/digit, 1--9. As opposed to any character, so if you encountered 0A... you would not want that 0 removed, right?

                        ? :)

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @JonB Well, he can adjust the regular expression for his needs.
                        Also I don't think your statement is correct: "means any 0 before any other numeric character/digit".
                        Take a look at his examples and what expected results should be: "001" and "0101", so it become 1 and 101

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        JonBJ 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @JonB Well, he can adjust the regular expression for his needs.
                          Also I don't think your statement is correct: "means any 0 before any other numeric character/digit".
                          Take a look at his examples and what expected results should be: "001" and "0101", so it become 1 and 101

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

                          @jsulm

                          1. The point is you can't do your way (str.remove()) if you need look ahead/behind :)
                          2. I don't know what you mean by "Also I don't think your statement is correct:", I think my statement is correct, you must be mis-interpreting it? Yes, my interpretation would mean 001 -> 1 and 0101 -> 101. It would also mean 0A -> 0A, which your reg ex remove would not, hence I was asking OP for clarification :)

                          Chances are OP only ever has 0 followed by digits, only he knows, in which case yours does work, nice and simple. But it's not what he has said/promised so far!

                          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