2013-02-20 73 views
1

您知道如何獲得QString中可能參數的計數嗎?計數QString參數

我想要做的事,如:

int argumentCount = countArguments(QString("This is the %1 argument, this is the %2 one")`); 

它的結果應該是argumentCount == 2

回答

4

您可以使用regular expressionsQString::count功能:

QString str1("%1%2 test test %3 %4 %555"); 
int n = str1.count(QRegExp("%\\d+"));//n == 5 

更新: 因爲QString的的ARG號可以在1-99範圍內這REG-EXP可用於:

QString str1("%1%2 test test %3 %4 %555"); 
int n = str1.count(QRegExp("%\\d{1,2}(?!\\d)"));//n == 4 
+0

哇。我真的需要了解更多關於它們的信息...... *視域* 最後一個問題...... QString :: arg()的參數範圍是%1-99;怎麼做? – Zaiborg 2013-02-20 20:06:45

+0

我更新了我的答案。 – saeed 2013-02-20 20:40:24

+0

感謝隊友,這讓事情變得如此簡單:D – Zaiborg 2013-02-20 20:42:55