QString argument templates not understood

47 Views Asked by At

I have a QString template with static numbers, mixed in with argument/template placeholders like %1 and %2. For example:

QString("123%1%26789").arg("4").arg("5")

The %1 should be replaced with 4, and %2 should be replaced with 5. But the second placeholder confuses C++/Qt because the placeholder can't separate the %2 from the neighboring 6.

I tried %{2} and other silly things, but no luck. Is there a ways to achieve this without create two statements, appending the remainder of the string in the second statement:

QString("123%1%2").arg("4").arg("5")+QString("6789")

The results should be:

123456789
1

There are 1 best solutions below

1
ZAIN UL ABIDEEN On

QString("123%1%%2%789").arg("4").arg("6")

Here, the first placeholder (%1) will be replaced with "4", and the second placeholder (%2) will be replaced with "6". The %% in between them will be interpreted as a literal percent sign and will not be replaced.

This will result in a QString "1234%6789", which has the desired replacements.