Re: Searching a QString inside another QString (not using string literals)
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:
Code:
QString temp
= "hello world, hello every one";
bool a = temp.contains("hello world");
//now bool is true
If I do:
Code:
QString temp
= "hello world, hello every one";
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:
Code:
(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
Re: Searching a QString inside another QString (not using string literals)
Re: Searching a QString inside another QString (not using string literals)
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?
Re: Searching a QString inside another QString (not using string literals)
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:
Code:
int main(int argc, char *argv[])
{
qWarning() << temp.contains(match);
return 0;
}
Re: Searching a QString inside another QString (not using string literals)
Nothing..
The problem is that I build my string from a previous one:
Code:
....//some code
biggerFile.replace("_", " ");
//now bigger file contains spaces instead of underscores and it always fails the check
//now I try
//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..
Re: Searching a QString inside another QString (not using string literals)
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.
Re: Searching a QString inside another QString (not using string literals)
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..
Re: Searching a QString inside another QString (not using string literals)
Quote:
Originally Posted by
shaolin
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.
Re: Searching a QString inside another QString (not using string literals)
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!