c++ - Extract number from Alphanumeric QString -
i have qstring of "s150 d300". how can numbers qstring , convert integer. using 'toint' not working.
let say, qstring of "s150 d300", number after alphabet 'd' meaningful me. how can extract value of '300' string?
thank time.
one possible solution use regular expressions shown below:
#include <qcoreapplication> #include <qdebug> int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); qstring str = "s150 dd300s150 d301d302s15"; qregexp rx("d(\\d+)"); qlist<int> list; int pos = 0; while ((pos = rx.indexin(str, pos)) != -1) { list << rx.cap(1).toint(); pos += rx.matchedlength(); } qdebug()<<list; return a.exec(); }
output:
(300, 301, 302)
thanks comment of @ilbeldus, , according information qregexp deprecated, propose solution using qregularexpression
:
another solution:
qstring str = "s150 dd300s150 d301d302s15"; qregularexpression rx("d(\\d+)"); qlist<int> list; qregularexpressionmatchiterator = rx.globalmatch(str); while (i.hasnext()) { qregularexpressionmatch match = i.next(); qstring word = match.captured(1); list << word.toint(); } qdebug()<<list;
output:
(300, 301, 302)
Comments
Post a Comment