Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Architecture, Design & Strategy

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Why English is really important to us non-english speakers.How to write a simple dependency resolver webcast »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

I am watching another dnrTV webcast, this time featuring Kathleen Dollard. The webcast is about the difference between VB.Net and C# for the .Net 3.5 framework.

An interesting point came up where Kathleen says that casting to lower with unicode can be a security risk, more so then casting to upper.

She was talking about an article in Visual Studio magazine by Bill McCarthy about this subject but I can’t find it.

Anyway, it is always better to use string.EqualsIgnoreCase("test") than it is to do string.ToLower.Equals("test"). I’ll just take her word for it ;-).

EDIT: Apparently Remou has beter Google skills than me.

And here is his explanation from that page.

It depends a lot on the locale. For example, in some locales é will uppercase to E, yet E will lowercase to e. If you compare the lowercase of E to é, it won’t be a match. Uppercase, on the other hand, will generally match. The use of ToUpper can be handy in circumstances like this one:

Select Case myString.ToUpper
Case “ABC”
Case “DEF", “FGH”
Case “IJK”
‘ …

You can also specify the culture to use:

mystring.ToUpper
(Globalization.CultureInfo.InvariantCulture)

For the most part, using ToUpper won’t cause any problems, but the ToUpper call does cause a new string to be created, so it can get expensive if you use it repeatedly. With some locales, and with special characters such as the German letter ß, ToUpper won’t give the same results as the Compare or Equals methods. That’s why I said in the article to consider using String.Compare instead. Consider how things change if we write the ToUpper code sample using String.Compare or String.Equals:

If String.Equals(myString, “ABC",
StringComparison.OrdinalIgnoreCase) Then
ElseIf String.Equals(myString, “DEF",
StringComparison.OrdinalIgnoreCase) _
OrElse String.Equals(myString, “FGH",
StringComparison.OrdinalIgnoreCase) _
Then
ElseIf String.Equals(myString, “IJK",
StringComparison.OrdinalIgnoreCase) Then
‘ …
End If

I think I’d much rather suffer a little performance hit for the sake of readability.

About the Author

User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
1115 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

3 comments

Comment from: remou [Member] Email
***--
Perhaps it is this response to a letter: http://visualstudiomagazine.com/columns/article.aspx?editorialsid=2334
28/10/08 @ 07:06
Comment from: thatrickguy [Member] Email
Most of the time when we're checking a string for equality, it's not a huge string. So the "performance hit" of creating a new string is insignificant.

Even in a heavily used procedures, loops, or recursion, the odds of ever having a short string generation due to .ToUpper costing more time than it would take for a new developer to read and understand the huge if/elseif block with string.compare is negligible.

The only time memory optimizations to that extent would really benefit us is in situations where we are not likely developing in .Net anyway ;)

That said, if you are going to be dealing with various cultures, character sets, and those squigly line characters most of us Americans can't pronounce... yeah, string.compare is going to be the better option. Although, scrap the if/elseif and try something like:

select case true
case String.Equals(myString, “ABC",
StringComparison.OrdinalIgnoreCase)
'do something
case String.Equals(myString, “DEF",
StringComparison.OrdinalIgnoreCase)
'do something else
'...
end select

-Rick
28/10/08 @ 08:00
Comment from: SQLDenis [Member] Email
*****
I always said that everyone should switch to Esperanto, metric system and GMT, that would eliminate 99% of the problems today
28/10/08 @ 15:46

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)