Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

10. The Class Library, Part 1 > Comparing Strings

Comparing Strings

The value of a String is the text it contains, and in most cases comparisons work exactly as you’d expect them to:

image

But what if String1 contains “Hello” and String2 contains “hello”? Are they equal? By default, the answer is no, because the String structure performs an ORDINAL STRING OPERATION, which means, in effect, that it compares the numeric values of each Char in the string. You may have noticed that Windows Explorer sorts “File 20” before “File 5”. That’s because it does an ordinal comparison, and “2” is less than “5”.

But the String structure exposes overloads of most of its methods that accept a member of the StringComparison enumeration as a parameter. The StringComparison enumeration allows you to specify precisely what you want to have happen, and best practice dictates that you should always use one of these overloads, just to make your intentions perfectly clear. The StringComparison enumeration contains the following members:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint