Results 1 to 9 of 9

Thread: Searching a QString inside another QString (not using string literals)

  1. #1
    Join Date
    Nov 2010
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:

    Qt Code:
    1. QString temp = "hello world, hello every one";
    2. bool a = temp.contains("hello world");
    3. //now bool is true
    To copy to clipboard, switch view to plain text mode 

    If I do:

    Qt Code:
    1. QString temp = "hello world, hello every one";
    2. QString match = "hello world";
    3. bool a = temp.contains(match);
    4. //bool is ALWAYS false
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. (temp.toStdString().find(biggerFile.toStdString()) >= 0)
    To copy to clipboard, switch view to plain text mode 

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

    Thx,
    ShaoLin
    Last edited by shaolin; 24th November 2010 at 13:07.

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Searching a QString inside another QString (not using string literals)


  3. #3
    Join Date
    Nov 2010
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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?
    Last edited by shaolin; 24th November 2010 at 15:38.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QString temp = QString("hello world, hello every one");
    4. QString match = QString("hello world");
    5. qWarning() << temp.contains(match);
    6.  
    7. return 0;
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2010
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Searching a QString inside another QString (not using string literals)

    Nothing..
    The problem is that I build my string from a previous one:

    Qt Code:
    1. ....//some code
    2. biggerFile.replace("_", " ");
    3. //now bigger file contains spaces instead of underscores and it always fails the check
    4. //now I try
    5. QString matcher = QString(biggerFile);
    6. //it again fails the comparison. I can't build the string with a literal..
    To copy to clipboard, switch view to plain text mode 

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

  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.

  7. #7
    Join Date
    Nov 2010
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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..

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Searching a QString inside another QString (not using string literals)

    Quote Originally Posted by shaolin View Post
    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.

  9. #9
    Join Date
    Nov 2010
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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

    Thanks to evryone guys!

Similar Threads

  1. Putting unicode in QString literals
    By reddish in forum Qt Programming
    Replies: 3
    Last Post: 30th October 2012, 02:25
  2. Qstring vs string
    By cbarmpar in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2008, 22:23
  3. std::string to QString
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2008, 04:56
  4. Replies: 4
    Last Post: 31st January 2008, 21:44
  5. std::string to QString?
    By Backslash in forum Newbie
    Replies: 4
    Last Post: 1st August 2007, 16:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.