Get index of a SubString contained in a QStringList
-
Hello,
I come to you because I have the following problem:
I have a QStringList of Error and Warning. Warnings are written like this: "Warning: WARN_CODE" and error like this "Error: ERR_CODE"
In the QStringList there can only be one error, randomly placed in the list.
I'm looking for a way to make the error of this QStringList to always be at index 0.
One of the solutions I would have liked to implement would have been to find the index of the QString containing "ERROR" and simply put it in the first position, but the "indexOf" function only seems to work if the string is exactly equal to what i search.
But I just want to find the SubString "Error"I tried various method with QStringList::Filter or QStringList::indexOf without success. What would be the "clean" way to do this? Thanks in advance, have a good day
-
Hello,
I come to you because I have the following problem:
I have a QStringList of Error and Warning. Warnings are written like this: "Warning: WARN_CODE" and error like this "Error: ERR_CODE"
In the QStringList there can only be one error, randomly placed in the list.
I'm looking for a way to make the error of this QStringList to always be at index 0.
One of the solutions I would have liked to implement would have been to find the index of the QString containing "ERROR" and simply put it in the first position, but the "indexOf" function only seems to work if the string is exactly equal to what i search.
But I just want to find the SubString "Error"I tried various method with QStringList::Filter or QStringList::indexOf without success. What would be the "clean" way to do this? Thanks in advance, have a good day
@Pantoufle said in Get index of a SubString contained in a QStringList:
function only seems to work if the string is exactly equal to what i search
Well, your string contains Error and you're searching for ERROR, right? Why? But if you want to do so, then simply set the cs parameter in https://doc.qt.io/qt-6/qstring.html#indexOf to Qt::CaseInsensitive
-
Hello,
I come to you because I have the following problem:
I have a QStringList of Error and Warning. Warnings are written like this: "Warning: WARN_CODE" and error like this "Error: ERR_CODE"
In the QStringList there can only be one error, randomly placed in the list.
I'm looking for a way to make the error of this QStringList to always be at index 0.
One of the solutions I would have liked to implement would have been to find the index of the QString containing "ERROR" and simply put it in the first position, but the "indexOf" function only seems to work if the string is exactly equal to what i search.
But I just want to find the SubString "Error"I tried various method with QStringList::Filter or QStringList::indexOf without success. What would be the "clean" way to do this? Thanks in advance, have a good day
@Pantoufle
You can either-
Iterate the string list looking for
QString::indexOf()
in each one. -
Use qsizetype QStringList::indexOf(const QRegularExpression &re, qsizetype from = 0) const to find a suitable
QRegularExpression
which you construct to find your desired substring. Probably something like.*Error.*
.
-
-
Excuse me I wrote badly in the post but no I have "ERROR: ERR_XXX" in my QStringList and I'm looking for "ERROR" with indexof.
Example:QStringList errAndWarn; errAndWarn.append("WARNING: WARN_FFF"); errAndWarn.append("ERROR: ERR_FFF"); errAndWarn.append("WARNING: WARN_FFF"); std::cout << errAndWarn.indexOf("ERROR") << std::endl;
This little sample return me "-1" as indexOf search for the exact match.
-
@Pantoufle
You can either-
Iterate the string list looking for
QString::indexOf()
in each one. -
Use qsizetype QStringList::indexOf(const QRegularExpression &re, qsizetype from = 0) const to find a suitable
QRegularExpression
which you construct to find your desired substring. Probably something like.*Error.*
.
-
-
@Pantoufle
That because the argument is a regular expression and it saysReturns the index position of the first exact match of re in the list
Try my suggested reg ex, see how the
.*
gobble up other characters so that it (should) match exactly. Your case might also work withERROR:.*
. You may also want to make the reg exp case insensitive, depending on your inputs. -
Excuse me I wrote badly in the post but no I have "ERROR: ERR_XXX" in my QStringList and I'm looking for "ERROR" with indexof.
Example:QStringList errAndWarn; errAndWarn.append("WARNING: WARN_FFF"); errAndWarn.append("ERROR: ERR_FFF"); errAndWarn.append("WARNING: WARN_FFF"); std::cout << errAndWarn.indexOf("ERROR") << std::endl;
This little sample return me "-1" as indexOf search for the exact match.
@Pantoufle OK, you're searching in the list directly. Then do what @JonB suggested:
qDebug() << errAndWarn.indexOf(QRegularExpression(".*ERROR.*"));