QString::contains() not working properly
-
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? -
Hi,
How did you implement that ?
-
@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; } } }
-
@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; } } }
@Vinod-Kuntoji may we see the definition of cLinesList and data?
-
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?@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 thecLinesList
and what is the output of your code?In
line.contains(data.at(j))
, what is inline
and what is indata.at(j)
when you think it "does not work properly"? -
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.) -
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.)@hskoglund That's a helluva speech you just made up there
-
Thanks! I got my inspiration from a troop of monkeys writing the complete work of Wiliam Shakespeare :-)
-
@hskoglund said in QString::contains() not working properly:
QStringList slResult = cLinesList;
for (auto oneData : data)
slResult = slResult.filter(oneData);@hskoglund. Thank you so much :)
-
Hi @hskoglund,
Is it possible to find the line number in which data list is found? -
Hi @hskoglund,
Is it possible to find the line number in which data list is found?@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.
-
Hi @hskoglund,
Is it possible to find the line number in which data list is found?@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.