PDA

View Full Version : QStringlist contains() usage



vieraci
28th October 2009, 12:13
Can anyone correct me here ?


QStringList list;
list << "ABC" << "123";
QString testStr = "AB"
if (list.contains(testStr))
contains() ALWAYS returns false !
Also,

QRegExp rx(testStr+"*");
rx.setPatternSyntax(QRegExp::Wildcard);
if (list.indexOf(rx) != -1)
ALWAYS returns -1

What am I missing ?

^NyAw^
28th October 2009, 12:22
Hi,

Because conatins is comparing "AB" with "ABC" and "123" and "AB" is different to "ABC" and different to "123".

Maybe you want to try something like this:


QStringList list;
list << "ABC" << "123";
bool bFound = false;
int i = 0;
while ((!bFound) && (i<list.count()))
{
if (list.at(i).contains(QString("AB"))
bFound = true:
else
i++;
}
//Here you know the index(i) of the founded string


This code is not tested.

wysota
28th October 2009, 12:27
Or a slightly faster version:


bool listContains(const QStringList &list, const QString &str){
QStringMatcher matcher(str);
foreach(const QString &listitem, list){
if(matcher.indexIn(listitem)!=-1) return true;
}
return false;
}

^NyAw^
28th October 2009, 12:52
Hi,

Didn't know about QStringMatcher class. Good apointment

vieraci
28th October 2009, 13:23
Hi,

Didn't know about QStringMatcher class. Good apointment

Neither did I !

so, QStringList::contains() does not match partial strings huh ?

^NyAw^
28th October 2009, 14:52
Hi,

Please read my first reply on this post. I've explained it there.