Results 1 to 7 of 7

Thread: searching a QString in a QStringList

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default searching a QString in a QStringList

    Hi,
    There is a QStringList with "n" fields. I want to search a QString in all items of the QStringList and item at index position "i" must be returned. Case Sensitive is not important. even if that QString be inside in an item, that item must be returned.

    for example:

    Qt Code:
    1. list<< "Hello" << "lombo" << "Hi" << "hlok";
    2. //I want to search "lo" and need to find this in: "Hello", "lombo", "hlok" and returned items at index position 0, 1, 3
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,539
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: searching a QString in a QStringList

    Read about QStringList::indexOf methods.

  3. #3
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: searching a QString in a QStringList

    Thanks Lesiok,
    I used:
    Qt Code:
    1. list << "hello" << "heeeeee"<<"he" << "kreo" << "gry" << "hhh";
    2.  
    3. qDebug() << list.indexOf(QRegExp("he", Qt::CaseInsensitive, QRegExp::W3CXmlSchema11), 0);//I checked all of PatternSyntax but for all, it returns 2
    To copy to clipboard, switch view to plain text mode 

    but always returns 2. I need it returns 0 because "he" exists in "hello" too.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,539
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: searching a QString in a QStringList

    I think that it should be something like that
    Qt Code:
    1. list.indexOf(QRegExp(".*he.*", Qt::CaseInsensitive, QRegExp::W3CXmlSchema11), 0)
    To copy to clipboard, switch view to plain text mode 
    or simpler
    Qt Code:
    1. list.indexOf(QRegExp("*he*", Qt::CaseInsensitive, QRegExp::Wildcard), 0)
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Lesiok for this useful post:

    Alex22 (21st January 2016)

  6. #5
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: searching a QString in a QStringList

    Lesiok, Thanks in advance
    Qt Code:
    1. list <<"dcsdcsdcscsc"<<"jjjjj" <<"hghghghegggg"<<"hello" << "heeeeee"<<"he" << "kreo" << "gry" << "hhh";
    2.  
    3. for(int i=0; i<9; i++)
    4. {
    5. if(list.indexOf(QRegExp("*he*", Qt::CaseInsensitive, QRegExp::Wildcard), i)-i == 0)
    6. qDebug() <<list.indexOf(QRegExp("*he*", Qt::CaseInsensitive, QRegExp::Wildcard), i);
    7. }
    To copy to clipboard, switch view to plain text mode 

    works well.

  7. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,539
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: searching a QString in a QStringList

    Optimal code looks like this
    Qt Code:
    1. list <<"dcsdcsdcscsc"<<"jjjjj" <<"hghghghegggg"<<"hello" << "heeeeee"<<"he" << "kreo" << "gry" << "hhh";
    2.  
    3. for(int i=0; i<list.size(); i++)
    4. {
    5. i = list.indexOf(QRegExp("*he*", Qt::CaseInsensitive, QRegExp::Wildcard), i);
    6. if( i < 0 )
    7. break;// QStringList::indexOf returns -1 if not found
    8.  
    9. qDebug() << i;
    10. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to Lesiok for this useful post:

    Alex22 (21st January 2016)

  9. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: searching a QString in a QStringList

    Even more optimized code would avoid recomputing the QRegExp and list.size() on each iteration of the loop.

Similar Threads

  1. QStringlist To QString
    By phillip_Qt in forum Qt Programming
    Replies: 6
    Last Post: 19th June 2015, 14:17
  2. QString into QStringList
    By kais khelifa in forum Newbie
    Replies: 2
    Last Post: 14th November 2011, 22:02
  3. Replies: 8
    Last Post: 25th November 2010, 11:40
  4. QString to QStringList
    By been_1990 in forum Newbie
    Replies: 8
    Last Post: 7th May 2010, 15:34
  5. QMAP<QString, QStringList>
    By bismitapadhy in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2010, 04:47

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
  •  
Qt is a trademark of The Qt Company.