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
QtWS25 Last Chance

remove char from qstring

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 6.7k 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.
  • S SGaist
    20 Feb 2020, 19:33

    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 20 Feb 2020, 19:50 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

    J J 2 Replies Last reply 20 Feb 2020, 19:55
    0
    • S s002wjh
      20 Feb 2020, 19:50

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

      J Offline
      J Offline
      JonB
      wrote on 20 Feb 2020, 19:55 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
      • G Offline
        G Offline
        GabrielRR
        wrote on 20 Feb 2020, 21:48 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

        J 1 Reply Last reply 20 Feb 2020, 21:55
        0
        • G GabrielRR
          20 Feb 2020, 21:48

          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.

          J Offline
          J Offline
          JonB
          wrote on 20 Feb 2020, 21:55 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?

          G 1 Reply Last reply 20 Feb 2020, 22:00
          0
          • J JonB
            20 Feb 2020, 21:55

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

            G Offline
            G Offline
            GabrielRR
            wrote on 20 Feb 2020, 22:00 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
              20 Feb 2020, 19:50

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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 21 Feb 2020, 05:29 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

              A J 2 Replies Last reply 21 Feb 2020, 06:11
              2
              • J jsulm
                21 Feb 2020, 05:29

                @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+"));
                
                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 21 Feb 2020, 06:11 last edited by
                #9

                @jsulm Fixed it for you :)

                Qt has to stay free or it will die.

                J 1 Reply Last reply 21 Feb 2020, 06:12
                0
                • A aha_1980
                  21 Feb 2020, 06:11

                  @jsulm Fixed it for you :)

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 21 Feb 2020, 06:12 last edited by
                  #10

                  @aha_1980 Thanks! :-)

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

                  1 Reply Last reply
                  0
                  • J jsulm
                    21 Feb 2020, 05:29

                    @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+"));
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 21 Feb 2020, 08:08 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?

                    ? :)

                    J 1 Reply Last reply 21 Feb 2020, 08:15
                    0
                    • J JonB
                      21 Feb 2020, 08:08

                      @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?

                      ? :)

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 21 Feb 2020, 08:15 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

                      J 1 Reply Last reply 21 Feb 2020, 08:18
                      0
                      • J jsulm
                        21 Feb 2020, 08:15

                        @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

                        J Offline
                        J Offline
                        JonB
                        wrote on 21 Feb 2020, 08:18 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

                        12/13

                        21 Feb 2020, 08:15

                        • Login

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