Code style Tip: Case insensitive string comparison
Posted
by Michael Freidgeim
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Michael Freidgeim
Published on Sun, 15 Apr 2012 01:34:04 GMT
Indexed on
2012/04/15
5:31 UTC
Read the original article
Hit count: 545
Filed under:
Good
if (String.Compare(myString, ALL_TEXT, StringComparison.OrdinalIgnoreCase) == 0)
{
return true;
}
OK(not obvious what true means)
if (String.Compare(myString, ALL_TEXT, true) == 0)
{
return true;
}
BAD: (non null safe)
if (myString.ToLower()==ALL_TEXT.ToLower()
{
return true;
}
if (String.Compare(myString, ALL_TEXT, StringComparison.OrdinalIgnoreCase) == 0)
{
return true;
}
OK(not obvious what true means)
if (String.Compare(myString, ALL_TEXT, true) == 0)
{
return true;
}
BAD: (non null safe)
if (myString.ToLower()==ALL_TEXT.ToLower()
{
return true;
}
© Geeks with Blogs or respective owner