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. QString::contains() not working properly
Forum Update on Monday, May 27th 2025

QString::contains() not working properly

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 7 Posters 1.2k Views
  • 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.
  • Vinod KuntojiV Offline
    Vinod KuntojiV Offline
    Vinod Kuntoji
    wrote on last edited by
    #1

    I have 4 strings, I need to search these strings in file line by line.
    I need to return the line number which contains all the 4 strings. I tried using string::contains(), but In loop string::contains not working properly. How can I return the exact line number which contains all the 4 strings?

    C++, Qt, Qt Quick Developer,
    PthinkS, Bangalore

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How did you implement that ?

      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
      1
      • Vinod KuntojiV Offline
        Vinod KuntojiV Offline
        Vinod Kuntoji
        wrote on last edited by
        #3

        @SGaist ,
        I am using like this.

                for(int i = 0;i < cLinesList.size(); i++)
                {
                    QString line = cLinesList.at(i);
                    int c = 0;
                    int s = data.size();
                    for(int j = 0; j < data.size(); j++)
                    {
                        if(line.contains(data.at(j)))
                        {
                            qDebug() << "line no :" << i << "\tindex :" << index << "\tData :" << data.at(j) << endl;
                            ++c;
                        }
                    }
                }

        C++, Qt, Qt Quick Developer,
        PthinkS, Bangalore

        mzimmersM 1 Reply Last reply
        0
        • Vinod KuntojiV Vinod Kuntoji

          @SGaist ,
          I am using like this.

                  for(int i = 0;i < cLinesList.size(); i++)
                  {
                      QString line = cLinesList.at(i);
                      int c = 0;
                      int s = data.size();
                      for(int j = 0; j < data.size(); j++)
                      {
                          if(line.contains(data.at(j)))
                          {
                              qDebug() << "line no :" << i << "\tindex :" << index << "\tData :" << data.at(j) << endl;
                              ++c;
                          }
                      }
                  }
          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

          @Vinod-Kuntoji may we see the definition of cLinesList and data?

          1 Reply Last reply
          0
          • Vinod KuntojiV Vinod Kuntoji

            I have 4 strings, I need to search these strings in file line by line.
            I need to return the line number which contains all the 4 strings. I tried using string::contains(), but In loop string::contains not working properly. How can I return the exact line number which contains all the 4 strings?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Vinod-Kuntoji said in QString::contains() not working properly:

            I tried using string::contains(), but In loop string::contains not working properly.

            What is the value of data you are searching for, what is in the cLinesList and what is the output of your code?

            In line.contains(data.at(j)), what is in line and what is in data.at(j) when you think it "does not work properly"?

            1 Reply Last reply
            0
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              Just guessing biut if both cLinesList and data are of the type QStringList, then you could use the filter function, say like this:

              QStringList cLinesList =
              {
              "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.",
              "Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.",
              "We are met on a great battle-field of that war.",
              "We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.",
              "It is altogether fitting and proper that we should do this.",
              "But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground.",
              "The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.",
              "The world will little note, nor long remember what we say here, but it can never forget what they did here.",
              "It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.",
              "It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth."
              };
              
              QStringList data = {"here","we","world","they"};
              
              QStringList slResult = cLinesList;
              for (auto oneData : data)
                  slResult = slResult.filter(oneData);
              

              slResult will contain only the line(s) that have all the four words.
              (If you need the line no. you could insert it first on every line.)

              JonBJ 1 Reply Last reply
              4
              • hskoglundH hskoglund

                Just guessing biut if both cLinesList and data are of the type QStringList, then you could use the filter function, say like this:

                QStringList cLinesList =
                {
                "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.",
                "Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.",
                "We are met on a great battle-field of that war.",
                "We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.",
                "It is altogether fitting and proper that we should do this.",
                "But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground.",
                "The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.",
                "The world will little note, nor long remember what we say here, but it can never forget what they did here.",
                "It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.",
                "It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth."
                };
                
                QStringList data = {"here","we","world","they"};
                
                QStringList slResult = cLinesList;
                for (auto oneData : data)
                    slResult = slResult.filter(oneData);
                

                slResult will contain only the line(s) that have all the four words.
                (If you need the line no. you could insert it first on every line.)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @hskoglund That's a helluva speech you just made up there

                1 Reply Last reply
                0
                • hskoglundH Offline
                  hskoglundH Offline
                  hskoglund
                  wrote on last edited by
                  #8

                  Thanks! I got my inspiration from a troop of monkeys writing the complete work of Wiliam Shakespeare :-)

                  1 Reply Last reply
                  3
                  • Vinod KuntojiV Offline
                    Vinod KuntojiV Offline
                    Vinod Kuntoji
                    wrote on last edited by
                    #9

                    @hskoglund said in QString::contains() not working properly:

                    QStringList slResult = cLinesList;
                    for (auto oneData : data)
                    slResult = slResult.filter(oneData);

                    @hskoglund. Thank you so much :)

                    C++, Qt, Qt Quick Developer,
                    PthinkS, Bangalore

                    1 Reply Last reply
                    0
                    • Vinod KuntojiV Offline
                      Vinod KuntojiV Offline
                      Vinod Kuntoji
                      wrote on last edited by
                      #10

                      Hi @hskoglund,
                      Is it possible to find the line number in which data list is found?

                      C++, Qt, Qt Quick Developer,
                      PthinkS, Bangalore

                      Christian EhrlicherC M 2 Replies Last reply
                      0
                      • Vinod KuntojiV Vinod Kuntoji

                        Hi @hskoglund,
                        Is it possible to find the line number in which data list is found?

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Vinod-Kuntoji said in QString::contains() not working properly:

                        Is it possible to find the line number in which data list is found?

                        Iterate over your QStringList and remember the indexes where you found your strings.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1
                        • Vinod KuntojiV Vinod Kuntoji

                          Hi @hskoglund,
                          Is it possible to find the line number in which data list is found?

                          M Offline
                          M Offline
                          mpergand
                          wrote on last edited by mpergand
                          #12

                          @Vinod-Kuntoji said in QString::contains() not working properly:

                          Is it possible to find the line number in which data list is found?

                          int QList::indexOf(const T &value, int from = ...)
                          Returns the index position of the first occurrence of value in the list, searching forward from index position from. Returns -1 if no item matched.

                          1 Reply Last reply
                          1

                          • Login

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