如何处理“%1"在QString :: arg()的参数中? [英] How to deal with "%1" in the argument of QString::arg()?

查看:960
本文介绍了如何处理“%1"在QString :: arg()的参数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都爱

QString("Put something here %1 and here %2")
    .arg(replacement1)
    .arg(replacement2);

,但是一旦您有最微妙的机会replacement1实际上包含%1甚至%2,事情就会变得发痒.然后,第二个QString::arg()将仅替换重新引入的%1或两个%2出现.无论如何,您不会获得您可能想要的原义"%1".

but things get itchy as soon as you have the faintest chance that replacement1 actually contains %1 or even %2 anywhere. Then, the second QString::arg() will replace only the re-introduced %1 or both %2 occurrences. Anyway, you won't get the literal "%1" that you probably intended.

有没有标准的技巧可以克服这个问题?

Is there any standard trick to overcome this?

如果您需要一个例子来玩,那就试试

If you need an example to play with, take this

#include <QCoreApplication>
#include <QDebug>

int main()
{
    qDebug() << QString("%1-%2").arg("%1").arg("foo");
    return 0;
}

这将输出

"foo-%2"

代替

"%1-foo"

可能是预期的(不是).

as might be expected (not).

    qDebug() << QString("%1-%2").arg("%2").arg("foo");

给予

"foo-foo"

    qDebug() << QString("%1-%2").arg("%3").arg("foo");

给予

"%3-foo"

推荐答案

请参阅有关

这篇关于如何处理“%1"在QString :: arg()的参数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆