PDA

View Full Version : Searching a QString inside another QString (not using string literals)



shaolin
24th November 2010, 13:07
Hi everyone, I'm new to this forum (but not to QT programming).
I'm facing this problem: I wanto to check if a particular string is contained in another string. If I do:


QString temp = "hello world, hello every one";
bool a = temp.contains("hello world");
//now bool is true

If I do:


QString temp = "hello world, hello every one";
QString match = "hello world";
bool a = temp.contains(match);
//bool is ALWAYS false

It seems that QString::contains() or QString::startsWith() and so on work only with string literals. Same results if I try to create a QRegExp. If I create it with a string literal it works, otherwise it doesn't. Same thing with QStringMatcher.

Any suggestion on how to avoid a string search char by char? Is it normal that such an important QT module works only with literals?

Thanks,
ShaoLin

Added after 38 minutes:

Solved using:


(temp.toStdString().find(biggerFile.toStdString()) >= 0)

that is standard c++ string pattern matching. Anyway, is there a QT solution for this kind of problem?

Thx,
ShaoLin

MarekR22
24th November 2010, 14:44
check: QString::indexOf

shaolin
24th November 2010, 15:24
Same thing. Maybe the problem is that tha string to be matched has a blank space inside, it's made by 2 words..

edit: yes, I found that the problem doesn't appear if I search for a single word. Any ideas?

Lykurg
24th November 2010, 15:43
Hm, works fine on my machine. Maybe you can try to use the QString constructor instead of using the auto convert function of Qt. Try:

int main(int argc, char *argv[])
{
QString temp = QString("hello world, hello every one");
QString match = QString("hello world");
qWarning() << temp.contains(match);

return 0;
}

shaolin
24th November 2010, 16:02
Nothing..
The problem is that I build my string from a previous one:


....//some code
biggerFile.replace("_", " ");
//now bigger file contains spaces instead of underscores and it always fails the check
//now I try
QString matcher = QString(biggerFile);
//it again fails the comparison. I can't build the string with a literal..

edit: I also tried with a new() and a pointer, but nothing..

MarekR22
25th November 2010, 11:08
I'm pretty sure that you have strip the problem and we don't see what source of your problem.
Test with code you have given at beginning are passing fine!

I suspect that your real code is using some different language (not English) and your problem is in fact problem of conversion from c-string to unicode (used in QString).
So please show us your real code not its translation to English!
Check also QTextCodec::setCodecForCStrings.

shaolin
25th November 2010, 12:16
The strings I'm handling are just file names, and I have to check if a given file name contains a certain substring, so it's indipendent from language locale I suppose..

Lykurg
25th November 2010, 12:23
so it's indipendent from language locale I suppose.. I don't think so. On my computer I can name files like

blödsinn.txt
טיפשות.txt
废话.txt

so the local can be important. On which environment you are facing that problem.

shaolin
25th November 2010, 12:40
Oh yes you are right, I was meaning european alphabet. Anyway it works perfectly, there was a really stupid but subtle error, and so the comparison used to fail..

Sorry :o

Thanks to evryone guys!