PDA

View Full Version : FindOneOf equalant in Qt



rajeshs
3rd December 2007, 11:40
Hi all,

I need replacement for FindOneOf(charSet) function of CString in QString,

This function Searches the string for the first character that matches any character contained in charSet and will return its position,

Eg:

CString s( "abcdef" );
s.FindOneOf( "sd" );

this will return 3 because d is the first matching character that is in 3rd Position.


If anybody knows please help me?

wysota
3rd December 2007, 11:48
QString str = "abcdef";
int i = str.indexOf(QRegExp("[sd]"));

elcuco
3rd December 2007, 17:06
QString str = "abcdef";
int i = str.indexOf(QRegExp("[sd]"));

dud, don't you think this costs too much...? I think iterating over the string and using QString::indexOf(QChar) will be cheaper.

wysota
3rd December 2007, 17:24
I don't think so, but you may perform some tests to find out. Using your method you have a complexity of O(n*m) where n is the length of string and m is the number of characters you want to test against. I don't think a regular expresion will do worse. It might not be better for such a simple case, but shouldn't be worse. The more complex the expression, the better performance it gives over simple string comparison.

rajeshs
4th December 2007, 11:58
using indexof() we can pass only character,

For my need regular expression is needed,

Thank you for your reply, and suggestions,

Now i got clear picture.

wysota
4th December 2007, 18:22
using indexof() we can pass only character,

For my need regular expression is needed,

Not quite. You can call indexOf multiple times with different characters and obtain the same result as with using a regular expression.