Manipulating Strings in C#—Comparing Strings
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
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. Typeif (s1 == s2) { }in whichs1ands2are string variables you wish to compare for equality (Figure 4.13).
- Use the

- Figure 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 whichstr1andstr2are the strings you wish to compare and true means ignore casing (falsemeans make a case sensitive comparison).
- 2. Test the result of the
Comparefunction. If result = 0 then the strings are equal. If the result is a negative number, thenstr1comes beforestr2alphabetically. If the result is a positive number, thenstr1comes afterstr2alphabetically (Figure 4.14).

- Figure 4.14. The
Comparemethod can be used to compare equality. WithCompareyou can control whether the comparison is case sensitive or not.Compareperforms 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
str1comes beforestr2in the ASCII table. If the result is a positive number, thenstr1comes afterstr2in the ASCII table (Figure 4.15).

- Figure 4.15.
CompareOrdinalperforms a comparison based on the language's codepage. Thecodepageis a table that assigns a code to each character. In the Englishcodepage, capital letters come before lowercase letters.
Tips
- A string variable that is
nullisn't equal to a string variable that contains an empty string. When you have functions with string parameters a programmer may send you anullor an empty string, so it's a good idea to test for both (Figure 4.16). When you read theTextproperty from aTextBoxand theTextBoxis empty,Textwill return an empty string, not anull.
- A string variable that is

- Figure 4.16. Because string variables could be
null, it's a good idea to make sure that the variable isn'tnulland 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 theCompareorCompareOrdinalfunctions (Figure 4.17).
- You can't use the

- 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.
-
CompareOrdinalis always case sensitive.Comparecan be either case sensitive (last parameter equal tofalse) or case insensitive (last parameter equals totrue) (Figure 4.18).
-

- Figure 4.18. The last parameter in
Comparecan 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 classEqualsdoes the same thing as==namely, it performs a case-sensitive test (Figure 4.19).
- The string class also has a function called
|


