John Harrison schrieb:
"Nils O. Selåsdal" <NO*@Utel.no> wrote:
Is there some quick C++ way I can do something similar to string::find ,
but case insensitive ?
You can use the predicate version of std::search
bool ci_equal(char ch1, char ch2)
{
return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
}
Caveat: that predicate will not work reliably for non-English strings.
e.g. in French accents are usually stripped off of capital letters, so
an 'E' can be equivalent (equal modulus case) to either 'e', 'é', 'è',
or 'ê'. In German, the letter 'ß' only exists in lower case, and the
correct capitalisation is "SS", while the reverse conversion is
ambiguous (some "SS" convert to "ss", others to 'ß').
It still doesn't catch all of those issues, but in most cases tolower()
is the better variant in my experience.
While 100% correct case conversions cannot be done in some languages
without knowledge of their spelling rules and exceptions to them, the
'ß' case IMHO shows that signatures like charT toupper( charT ) have
been designed with ignorance (no offence) towards languages where a
1-char-to-1-char conversion is not possible. Wide character support
doesn't alleviate this at all :-(((
Standardised whole-string case conversion functions would be deerly
needed, even if it's left to the implementation or the application to
implement them for a particular locale.
Regards,
Malte