PDA

View Full Version : How to check if a string starts with a substring?



lni
17th April 2007, 22:14
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

J-jayz-Z
17th April 2007, 22:34
Use QString::indexOf. If the function returns 0, it starts with the other string.
Like this snippet:

#include <QApplication>
#include <QString>
#include <QtDebug>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QString foo("foobarblubb");
QString bar("foo");
QString blubb("bla");
int index;
index = foo.indexOf(bar);
qDebug() << index;
index = foo.indexOf(blubb);
qDebug() << index;
return app.exec();
}

lni
17th April 2007, 22:49
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...

wysota
18th April 2007, 00:36
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.