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. [SOLVED] String formating %1%2 vs %2%1
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] String formating %1%2 vs %2%1

Scheduled Pinned Locked Moved General and Desktop
23 Posts 4 Posters 7.2k 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
    Salvatello
    wrote on last edited by
    #6

    I did not understand

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gkavrecic
      wrote on last edited by
      #7

      As I'll have a few minutes of time I'll make minimalistic source to reproduce this

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frankiefrank
        wrote on last edited by
        #8

        Could you paste your code exactly as it is?

        "Roads? Where we're going, we don't need roads."

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gkavrecic
          wrote on last edited by
          #9

          Frankie

          I'll put together a simple code to reproduce the issue on a smaller scale.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Saugglocke
            wrote on last edited by
            #10

            Hi gkavrecic,

            its because arg looks for the smallest place marker, like %1, %2 ... if you replace the first marker with a digit, e. g. 3 it will find the next marker as # and arg will replace the marker (#) with "D".

            best regards

            PS: why was the rest of my post missing?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gkavrecic
              wrote on last edited by
              #11

              So, this means that the markers (when concatenated) have to be in ascending order?

              Is this described anywhere?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Saugglocke
                wrote on last edited by
                #12

                Hm, whats wrong with the forum? % 23 gets replaced with #??

                Look here: http://qt-project.org/doc/qt-4.8/qstring.html#arg

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gkavrecic
                  wrote on last edited by
                  #13

                  Nothing wrong with the forum.

                  According to docs (also your link):
                  ...each arg() will still replace the lowest numbered unreplaced place marker, no matter where it appears...

                  According to my experience this is not true.

                  I'm going to write the test appl now.
                  I still might find my bug doing it...

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Saugglocke
                    wrote on last edited by
                    #14

                    Yeah, if you replace the first occurence in %2%1 with a digit the new marker will be %2x, where x is the digit. So the new smallest marker is 21, 22 depending on what digit x has been.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gkavrecic
                      wrote on last edited by
                      #15

                      ok, here is the minimalistic appl to prove this.

                      @#include <QtCore/QCoreApplication>
                      #include <QDebug>

                      int main(int argc, char *argv[])
                      {
                      int i = 7;
                      QString s = "D";

                      QString a = QString("%1%2").arg(s).arg(i,-2);
                      QString b = QString("%2%1").arg(i,-2).arg(s);
                      
                      qDebug() << a;
                      qDebug() << b;
                      
                      return 0;
                      

                      }@

                      The output of this code is (copy/pasted from the console window):
                      @"D7 "
                      "D "@

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Saugglocke
                        wrote on last edited by
                        #16

                        Its working as expected, just try it this way:

                        @ QString b = QString("%2%1").arg(i,-2);
                        qDebug() << b; // = "% 27"
                        b = b.arg(s); // now arg finds 27 as smalles marker and will replace it
                        qDebug() << b;@

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          gkavrecic
                          wrote on last edited by
                          #17

                          Saugglocke,
                          I was just thinking this before I refreshed the page...
                          It seems it has a very simplistic logic while converting it - after each argument it reuses the new string.

                          But with this logic one can't, for example, internationalise some monetary value like 5€ or $8 using the logic to switch places to value and currency...

                          You come back to position aware programming as it is in sprintf...
                          Not all the way, but still. You can't describe it as no matter where it appears

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gkavrecic
                            wrote on last edited by
                            #18

                            a reply to your sample code.
                            It does not work.

                            • the intermediate result is "%27 "

                            • the final one is "D " - same as before, just need more code

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              Saugglocke
                              wrote on last edited by
                              #19

                              args return value is a new string, hence the next arg will use the copy.

                              The sample was just to show the logic behind arg. It won't solve the problem. You could add a space character and replace it afterwards:

                              @QString b = QString("%2 %1").arg(i,-2).arg(s);
                              qDebug() << b.replace(" ", "");@

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                Salvatello
                                wrote on last edited by
                                #20

                                @ QString a = QString("%1%2").arg(s).arg(i,-2);@
                                is a correct form.

                                @ QString b = QString("%2%1").arg(i,-2).arg(s);@
                                not a correct form.

                                %1 must precede %2.
                                you can change the order of arg bat not the order of %n.

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  gkavrecic
                                  wrote on last edited by
                                  #21

                                  Saugglocke & Salvatello
                                  I understand what you want to say. And I solved my issues before I started to write this thread. I've just reordered the args as they appear in the string. I could solve it using sprintf the same way. For me is also easier to format the output with sprintf as it is easier and more logical.

                                  The second form of Salvatello is not wrong. In my opinion the library works wrong.

                                  I just wanted to check and put forward that the whole concept no matter where it appears falls as it can't be used as described. I can't really rely that the transtlatted string will work as expected.

                                  1 Reply Last reply
                                  0
                                  • F Offline
                                    F Offline
                                    frankiefrank
                                    wrote on last edited by
                                    #22

                                    I see what you're saying but I don't see how every separate call to .arg() with the single parameter could consider the case of the string replacement you describe here (the creation of & for example, which really reads as the 26th argument).

                                    Maybe what you should have done (or did already) is use the overload that replaces the strings in one pass?

                                    http://qt-project.org/doc/qt-5/qstring.html#arg-2
                                    @
                                    QString QString::arg(const QString & a1, const QString & a2) const
                                    @

                                    bq. This is the same as str.arg(a1).arg(a2), except that the strings a1 and a2 are replaced in one pass. This can make a difference if a1 contains e.g. %1

                                    Anyway, if you got to this or another solution, please post it and edit the post title to [SOLVED].

                                    "Roads? Where we're going, we don't need roads."

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      gkavrecic
                                      wrote on last edited by
                                      #23

                                      Hi Frankie,

                                      I have not. But seems the only way to make it work correctly.

                                      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