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.
  • S Offline
    S Offline
    sonichy
    wrote on 12 Jun 2018, 03:44 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

    K J 2 Replies Last reply 12 Jun 2018, 04:25
    0
    • P Offline
      P Offline
      Prince_0912
      wrote on 12 Jun 2018, 04:12 last edited by Prince_0912 6 Dec 2018, 04:15
      #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
      • S sonichy
        12 Jun 2018, 03:44

        Filter lines startWith "#include";

        QStringList list = textEdit->toPlainText().split("\n");
        QStringList result;
        result = list.filter(list.at(i).startWith("#include"));
        
        K Offline
        K Offline
        KillerSmath
        wrote on 12 Jun 2018, 04:25 last edited by KillerSmath 6 Dec 2018, 04:27
        #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
        • S sonichy
          12 Jun 2018, 03:44

          Filter lines startWith "#include";

          QStringList list = textEdit->toPlainText().split("\n");
          QStringList result;
          result = list.filter(list.at(i).startWith("#include"));
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 12 Jun 2018, 04:25 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
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 12 Jun 2018, 06:20 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
            • V Offline
              V Offline
              VRonin
              wrote on 12 Jun 2018, 09:12 last edited by VRonin 6 Dec 2018, 09:14
              #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

              5/6

              12 Jun 2018, 06:20

              • Login

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