Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to filter(startWith()) ?

    General and Desktop
    6
    6
    605
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • sonichy
      sonichy last edited by

      Filter lines startWith "#include";

      QStringList list = textEdit->toPlainText().split("\n");
      QStringList result;
      result = list.filter(list.at(i).startWith("#include"));
      

      https://github.com/sonichy

      KillerSmath jsulm 2 Replies Last reply Reply Quote 0
      • Prince_0912
        Prince_0912 last edited by Prince_0912

        Hi @sonichy ,
        list.filter(list.at(i).startWith("#include")); is return bool value so it is not convert bool to QString type.

        this below is helpful to you,
        bool QString::startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

        thank you.

        1 Reply Last reply Reply Quote 1
        • KillerSmath
          KillerSmath @sonichy last edited by KillerSmath

          @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.*"));
          
          ...
          

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply Reply Quote 5
          • jsulm
            jsulm Lifetime Qt Champion @sonichy last edited by

            @sonichy Use a regular expression:

            result = list.filter(QRegularExpression("^#include.*"));
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 4
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Hi,

              Please use QRegularExpression, QRegExp is to be considered deprecated with Qt 5.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 4
              • V
                VRonin last edited by VRonin

                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"));});
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply Reply Quote 4
                • First post
                  Last post