Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
This chapter is from the book

Comparing Strings

There are a number of ways to compare two strings. Normally developers want to know whether two strings are equal. When comparing two strings for equality, sometimes casing is important and sometimes it isn't (is Jose the same as jose? for example). When sorting a series of strings, developers also want to know if a string comes before another string. In that case there are a couple of ways of comparing the strings. You can use a dictionary comparison in which lowercase "a" comes before uppercase "C," or you can use an ASCII-like comparison (using a code page) in which all uppercase letters come before all lowercase letters ("C" comes before "a").

To perform a case-sensitive test for equality:

  • Use the == operator. Type if (s1 == s2) { } in which s1 and s2 are string variables you wish to compare for equality (Figure 4.13).

    Figure 4.13Figure 4.13 The == operator lets you compare two strings for equality. When you use == case sensitivity matters.

To perform a case-insensitive, dictionary-based string comparison:

  1. Type int result = string.Compare(str1,str2,true); in which str1 and str2 are the strings you wish to compare and true means ignore casing (false means make a casesensitive comparison).

  2. Test the result of the Compare function. If result = 0 then the strings are equal. If the result is a negative number, then str1 comes before str2 alphabetically. If the result is a positive number, then str1 comes after str2 alphabetically (Figure 4.14).

    Figure 4.14Figure 4.14 The Compare method can be used to compare equality. With Compare you can control whether the comparison is case sensitive or not. Compare performs a dictionary comparison.


To perform a case-sensitive, ASCII-based string comparison:

  1. Type int result = string.CompareOrdinal(str1,str2);

  2. Test the result of the Compare function. If result = 0, then the strings are equal. If the result is a negative number, then str1 comes before str2 in the ASCII table. If the result is a positive number, then str1 comes after str2 in the ASCII table (Figure 4.15).

    Figure 4.15Figure 4.15 CompareOrdinal performs a comparison based on the language's codepage. The codepage is a table that assigns a code to each character. In the English codepage, capital letters come before lowercase letters.


Tips

  • A string variable that is null isn't equal to a string variable that contains an empty string. When you have functions with string parameters a programmer may send you a null or an empty string, so it's a good idea to test for both (Figure 4.16). When you read the Text property from a TextBox and the TextBox is empty, Text will return an empty string, not a null.

    Figure 4.16Figure 4.16 Because string variables could be null, it's a good idea to make sure that the variable isn't null and not empty before using it.

  • You can't use the < or > operators with two strings. The only way to test for the order of two strings is to use the Compare or CompareOrdinal functions (Figure 4.17).

    Figure 4.17Figure 4.17 In my opinion, it's a shame that you can't compare strings with the < or > operators in C#, the way you can in other languages.

  • CompareOrdinal is always case sensitive. Compare can be either case sensitive (last parameter equal to false) or case insensitive (last parameter equals to true) (Figure 4.18).

    Figure 4.18Figure 4.18 The last parameter in Compare can be a little confusing. Instead of asking whether you want the comparison to be case sensitive or not, it asks whether you want the comparison to be case insensitive or not. So to do a case-sensitive search you have to resort to double negatives (not case insensitive).

  • The string class also has a function called Equals. Throughout this book you'll learn that every class has this function. In the string class Equals does the same thing as == namely, it performs a case-sensitive test (Figure 4.19).

    Figure 4.19Figure 4.19 Using the Equals method is similar to using the == operator.

  • + Share This
  • 🔖 Save To Your Account