This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# String Comparison Functions

The twenty-third part of the C# Fundamentals tutorial continues the examination of the string manipulation functionality provided by the String class. This article investigates methods available for comparing the contents of strings.

Equality and Inequality Operators

In an earlier part of the C# Fundamentals tutorial we discussed the equality (==) and inequality (!=) relational operators. These operators allow an exact, case-sensitive comparison of two strings. In the case of the equality operator, if the strings match, the resultant Boolean value is true. If they are different the result is false. The inequality operator returns the opposite result, as shown below:

string s1 = "String to compare.";
string s2 = "String to compare.";
string s3 = "String to Compare.";   // Note the capital 'C'
bool result;

result = s1 == s2;                  // result = true
result = s1 == s3;                  // result = false
result = s1 != s2;                  // result = false
result = s1 != s3;                  // result = true

These operators are useful but have limitations. They do not allow you to consider the equality of two strings that match in all but capitalisation or other cultural factors. Nor do they allow you to determine that one string is greater than another. This article explains the methods that are provided to solve these problems.

Relative Comparison

CompareTo Method

The .NET framework provides a method named CompareTo for many data types, including strings. It allows two values to be compared and returns a result that indicates not only if they are equal but also which is the greater when they are not. The method considers cultural information when comparing strings. For example, Japanese katakana characters can be written in two ways, half-width or full-width. The CompareTo method considers characters written as full-width to be equal to the same half-width characters. The same cultural information is used to determine which string is the greater.

The CompareTo method operates against an existing string. The string that it is to be compared with is passed to a parameter. The method returns an integer indicating the result of the comparison as follows:

Return ValueMeaning
ZeroThe strings are equal.
Less than ZeroThe first string is less than the second.
More than ZeroThe first is greater than the second or the second string is null.

This can be clarified with an example:

string animal1 = "Cat";
string animal2 = "Dog";
int result;

result = animal1.CompareTo("Cat");          // result is zero
result = animal2.CompareTo("Cat");          // result is greater than zero
result = animal1.CompareTo(animal2);        // result is less than zero

NB: As this method is called against an existing string instance, if the string is null an exception occurs:

string animal = null;
int result;

result = animal.CompareTo("Cat");           // Causes an exception

Compare Method

The Compare function is a static method of the string class. It provides similar functionality to CompareTo but allows more options to be specified. As a static member of the string class, both strings are passed as parameters.

string animal1 = "Cat";
string animal2 = "Dog";
int result;

result = String.Compare(animal1, "Cat");    // result is zero
result = String.Compare(animal2, "Cat");    // result is greater than zero
result = String.Compare(animal1, animal2);  // result is less than zero

Null Comparison

The CompareTo method raises an exception if the string that is being tested is null because a null object has no available methods. However, as the Compare method is static, null strings can be compared.

string animal = "Cat";
int result;

result = String.Compare(animal, null);      // result is greater than zero
result = String.Compare(null, animal);      // result is less than zero
result = String.Compare(null, null);        // result is zero

Case Sensitivity

The Compare method allows toy to decide whether to perform case-sensitive or case-insensitive comparisons. The differentiation between upper case and lower case lettering is applied according to the user's language settings. To determine whether to use case-sensitive comparison a third, Boolean parameter is used. If the value is true, character casing is ignored. If false, the effect is the same as not supplying the parameter at all; the test is case-sensitive.

This example assumes UK English settings:

string animal1 = "Cat";
string animal2 = "cat";                             // Note use of lower case
int result;

result = String.Compare(animal1, animal2, true);    // Strings are equal
result = String.Compare(animal1, animal2, false);   // Strings are not equal

The Compare method includes overloaded versions allowing the comparison of strings using further international language settings, as well as options for comparing parts of strings. These are beyond the scope of this tutorial but are worth exploring. More information can be found at the MSDN definition of String.Compare web page.

5 December 2006