Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to filter(startWith()) ?
Forum Updated to NodeBB v4.3 + New Features

How to filter(startWith()) ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 6 Posters 1.0k Views 1 Watching
  • 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    Filter lines startWith "#include";

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

    https://github.com/sonichy

    KillerSmathK jsulmJ 2 Replies Last reply
    0
    • Prince_0912P Offline
      Prince_0912P Offline
      Prince_0912
      wrote on last edited by Prince_0912
      #2

      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
      1
      • sonichyS sonichy

        Filter lines startWith "#include";

        QStringList list = textEdit->toPlainText().split("\n");
        QStringList result;
        result = list.filter(list.at(i).startWith("#include"));
        
        KillerSmathK Offline
        KillerSmathK Offline
        KillerSmath
        wrote on last edited by KillerSmath
        #3

        @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
        5
        • sonichyS sonichy

          Filter lines startWith "#include";

          QStringList list = textEdit->toPlainText().split("\n");
          QStringList result;
          result = list.filter(list.at(i).startWith("#include"));
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @sonichy Use a regular expression:

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

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

          1 Reply Last reply
          4
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            4
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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
              4

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved