Implement a Case INsensitive string.Comtains method?

  • Thread starter Thread starter Joe Cool
  • Start date Start date
Hi Pete,

Thanks for the great links.
Yes, you are correct. My method fails if pass with Turkish characters.
For example:
"HÄ° all".Contains("HI", CultureInfo.CurrentCulture) returns true where
it should return false.

After reading through your link and MSDN documentation about character
casing, now it becomes clear to me. For those who are interested to
know, this is what actually happens:
(I found that Unicode documentation also helps.
http://www.unicode.org/versions/Unicode5.0.0/ch05.pdf#G21180)

"HÄ° all".ToLower() is converted to "hi all". (Actually if follow unicode
documentation it becomes h+i+[U0307]+ +a+l+l)
"HI".ToLower() will become "hi".

Since "hi" is contained in "hi all", so the output returns true (which
is incorrect).

Pete's method use String.Equal with StringComparison option for
comparison. Under the hood, it use native API which takes care of the
above headache.

Lesson learned: Do not use ToLower() or ToUpper() for string comparison.

Regards.
 
Back
Top