c++ - How to specify the QString::indexOf method? -
i have written source code like:
int main(int argc, char *argv[]) { qstring x = "start text here end"; qstring s = "start"; qstring e = "end"; int start = x.indexof(s, 0, qt::caseinsensitive); int end = x.indexof(e, qt::caseinsensitive); if(start != -1){ // found qstring y = x.mid(start + s.length(), ((end - (start + s.length())) > -1 ? (end - (start + s.length())) : -1)); // if dont wanna pass in number less -1 or qstring y = x.mid(start + s.length(), (end - (start + s.length()))); // should not issues passing in number less -1, still works qdebug() << y << (start + s.length()) << (end - (start + s.length())); } }
the problem is, in textfile word "end" found very often. so, there way create indexof method searchs first " qstring e = "end" " appears after "qstring s = "start" " ? greetings
the declaration of indexof of qstring following:
int qstring::indexof ( const qstring & str, int = 0, qt::casesensitivity cs = qt::casesensitive ) const
if take you'll see there 1 more parameter 1 use in call indexof. because has default value , argument:
int = 0
this default set 0 whenever ommit value search done beginning of string, can set value index found "start" word this:
int start = x.indexof(s, 0, qt::caseinsensitive); int end = x.indexof(e, start, qt::caseinsensitive); //notice use of start 'from' argument
this way going index of first "end" word comes after first "start" word. hope helps!
Comments
Post a Comment