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.13 The == operator lets you compare two strings for equality. When you use == case sensitivity matters.
     string name1 = "James"; string name2 = "james"; if (  name1 == name2  )    Response.Write("same name"); else    Response.Write("not the same"); //prints out: not the same 

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.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.
     string name1 = "James"; string name2 = "james";  int result = string.Compare(name1,name2,true);  Response.Write( (  result == 0  ) ?                "same name<br>"              : "not the same<br>"); //prints out: same name name1 = "andrew"; name2 = "Bob";  result = string.Compare(name1,name2,true);  Response.Write( (  result < 0  ) ?                "andrew before Bob<br>"              : "Bob before andrew<br>"); //prints out: andrew before Bob 

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.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.
     string name1 = "James"; string name2 = "james";  int result =   string.CompareOrdinal(name1,name2);  Response.Write( (  result == 0  ) ?                "same name<br>"              : "not the same<br>"); //prints out: same name name1 = "andrew"; name2 = "Bob";  result = string.CompareOrdinal(name1,name2);  Response.Write( (  result < 0  ) ?                "andrew before Bob<br>"              : "Bob before andrew<br>"); //prints out: Bob before andrew 

graphics/tick.gif 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.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.
     private void Search(string whereClause) {    if (  whereClause != null && whereClause != ""  )    { //do something    }    else    {       //do something else    } } 
  • 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.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.
     string s1="James"; string s2="Andrew"; //*** this is illegal  *** //*** can't use < or > *** if (  s1 < s2  )   Response.Write("James before Andrew"); 
  • 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.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).
     string name1 = "James"; string name2 = "james";  //case-insensitive dictionary comparison  int result = string.Compare(name1,name2,  true  ); Response.Write( (result == 0) ?                "same name<br>"              : "not the same<br>"); //prints out: same name  //case-sensitive dictionary comparison  int result = string.Compare(name1,name2,  false  ); Response.Write( (result == 0) ?                "same name<br>"              : "not the same<br>"); //prints out: not the same 
  • 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.19 Using the Equals method is similar to using the == operator.
     string name1 = "James"; string name2 = "james"; if (  name1.Equals(name2)  )    Response.Write("same name"); else    Response.Write("not the same"); //prints out: not the same 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net