Is the QHash::contains method case-sensitive or case-incensitive?
Question
I have a QHash<QString,QString>
.
I use the following expression to check whether a value is there in the QHash::keys()
or not.
//programme
QHash<QString,QString> samplehash;
QString value = "somevalue";
if(samplehash.contains(value)) // Condition - 1
{
//some code
}
Sometimes the above conditions matches, sometimes not for the same letters of different case. Is the QHash::contains
method case-sensitive?
Solution
QHash.contains()
is case sensitive as John T mentioned. Without the code there is not much to figure out. You can imagine it doing a ==
between the keys.
Please do not forget that accessing a non existent element via []
will create an empty entry in the hash, this might be what causes your bug. contains
does not insert an entry into the hash, neither does value
OTHER TIPS
It is case sensitive. The common practice for handling data from multiple sources and comparing it is to convert it all to the same format first, which usually involves making everything lowercase prior to comparison.
This is a common practice, especially on websites for handling logins, or user input in applications to avoid the old 'PEBKAC' situations and make it easier for users.
One solution could be to subclass QString
and override the operator==
to do the comparison with case-insensitive mode, using QString::compare.
But the solution of John T is definitely better if it fits your constraints.