Results 1 to 4 of 4

Thread: How to check if a string starts with a substring?

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to check if a string starts with a substring?

    Hi,

    Sorry I have another question, how to check in pro file if a string starts with a substring?

    For instance, I have

    string1 = /foo/bar/blah/etc
    string2 = /foo/bar

    What function can I use to check if string1 starts with string2 or not?

    Many thanks.
    lni
    Last edited by lni; 17th April 2007 at 23:24.

  2. #2

    Default Re: How to check if a string starts with a substring?

    Use QString::indexOf. If the function returns 0, it starts with the other string.
    Like this snippet:
    Qt Code:
    1. #include <QApplication>
    2. #include <QString>
    3. #include <QtDebug>
    4.  
    5. int main(int argc, char* argv[])
    6. {
    7. QApplication app(argc, argv);
    8. QString foo("foobarblubb");
    9. QString bar("foo");
    10. QString blubb("bla");
    11. int index;
    12. index = foo.indexOf(bar);
    13. qDebug() << index;
    14. index = foo.indexOf(blubb);
    15. qDebug() << index;
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to check if a string starts with a substring?

    Thanks, but that is not the answer I am looking for.

    I am writing qmake's pro file, and in the pro file, I have two string variables, I need to check the condition within the pro file, it should look like:


    startsWith( $string1, $string2 ) {
    do-some-thing
    } else {
    do-other-thing...
    }

    I try both:

    contain( $string1, $string2 )

    ans = $$system( egrep $string1 | echo $string2 )

    neither seems to work...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to check if a string starts with a substring?

    RESULT = $$find(SOME_VAR, "^somestring")
    count(RESULT, 1){
    ...
    }

    You might also compose the regular expression if you need it instead of passing a hardcoded one.

Similar Threads

  1. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 21:43
  2. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 15:40

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.