How to filter(startWith()) ?
Solved
General and Desktop
-
-
@sonichy said in How to filter(startWith()) ?:
Filter lines startWith "#include";
QStringList list = textEdit->toPlainText().split("\n"); QStringList result; result = list.filter(list.at(i).startWith("#include"));
You can use a regular expression to find all strings starting with #include
#include <QStringList> #include <QRegExp> ... QStringList result; result = list.filter(QRegExp("^#include.*")); ...
-
Hi,
Please use QRegularExpression, QRegExp is to be considered deprecated with Qt 5.
-
Or if you are a parallel hipster:
Using Qt:
QStringList list = textEdit->toPlainText().split("\n"); QStringList result = QtConcurrent::blockingFiltered(list , [](const QString& val)->bool{return val.startWith(QLatin1String("#include"));});
using C++17:
QStringList list = textEdit->toPlainText().split("\n"); QStringList result; std::copy_if(std::execution::par,list.cbegin(), list.cend(), std::back_inserter(result), [](const QString& val)->bool{return val.startWith(QLatin1String("#include"));});