[SOLVED] How to sscanf integers from QString?
-
Hello I have a QString = "2014-12-23-23-34-45-<random integer 10 digits>".
The first six integers are year, month, day, hour, minute and second, while the last integer is a random one with 10 digits.
Could anybody tell me how to do extract year, month, day, hour, minute and second value from this QString and store them into six different integers? Thanks in advance.
-
Why not use QDateTime and solve it in a single line?
But, since you ask to have them separately, here you go:
@
QString string(“2014-12-23-23-34-45-<random integer 10 digits>”);
int year, month, day, hour, minute, second;year = string.left(4).toInt();
month = string.mid(5, 2).toInt();
day = string.mid(8, 2).toInt();
// ... and so on
@ -
Is the size and format does not change any time?? and what about hyphen(-)??? will it be there in all packects of data ? if so
if there is hyphen(which can be considered as delimiter), you can extract based on it's position.
else you dont have hyphen, then you can extract based on the static positions of "The first six integers are year, month, day, hour, minute and second, while the last integer is a random one with 10 digits"
-
A regular expression could also be used:
@
(\d{4})-(\d{1,2})-(\d{1,2})-(\d{1,2})-(\d{1,2})-(\d{1,2})-(\d{10})
@Just make sure to escape it properly when passing as string to a "QRegularExpression":http://qt-project.org/doc/qt-5/qregularexpression.html (or if you have Qt 4 "QRegExp":http://qt-project.org/doc/qt-4.8/qregexp.html,
With the regular expression supplied it will handle the case where say the month is jan and it is in the string as 1, not 01.