Compare strings in .NET

.NET provides several methods to compare the values of strings. The following table lists and describes the value-comparison methods.

Method name Use
String.Compare Compares the values of two strings. Returns an integer value.
String.CompareOrdinal Compares two strings without regard to local culture. Returns an integer value.
String.CompareTo Compares the current string object to another string. Returns an integer value.
String.StartsWith Determines whether a string begins with the string passed. Returns a Boolean value.
String.EndsWith Determines whether a string ends with the string passed. Returns a Boolean value.
String.Contains Determines whether a character or string occurs within another string. Returns a Boolean value.
String.Equals Determines whether two strings are the same. Returns a Boolean value.
String.IndexOf Returns the index position of a character or string, starting from the beginning of the string you are examining. Returns an integer value.
String.LastIndexOf Returns the index position of a character or string, starting from the end of the string you are examining. Returns an integer value.

Compare method

The static String.Compare method provides a thorough way of comparing two strings. This method is culturally aware. You can use this function to compare two strings or substrings of two strings. Additionally, overloads are provided that regard or disregard case and cultural variance. The following table shows the three integer values that this method might return.

Return value Condition
A negative integer The first string precedes the second string in the sort order.

-or-

The first string is null.
0 The first string and the second string are equal.

-or-

Both strings are null.
A positive integer

-or-

1
The first string follows the second string in the sort order.

-or-

The second string is null.

Important

The String.Compare method is primarily intended for use when ordering or sorting strings. You should not use the String.Compare method to test for equality (that is, to explicitly look for a return value of 0 with no regard for whether one string is less than or greater than the other). Instead, to determine whether two strings are equal, use the String.Equals(String, String, StringComparison) method.

The following example uses the String.Compare method to determine the relative values of two strings.

String^ string1 = "Hello World!";
Console::WriteLine(String::Compare(string1, "Hello World?"));
string string1 = "Hello World!";
Console.WriteLine(String.Compare(string1, "Hello World?"));
Dim string1 As String = "Hello World!"
Console.WriteLine(String.Compare(string1, "Hello World?"))

This example displays -1 to the console.

The preceding example is culture-sensitive by default. To perform a culture-insensitive string comparison, use an overload of the String.Compare method that allows you to specify the culture to use by supplying a culture parameter. For an example that demonstrates how to use the String.Compare method to perform a culture-insensitive comparison, see Culture-insensitive string comparisons.

CompareOrdinal method

The String.CompareOrdinal method compares two string objects without considering the local culture. The return values of this method are identical to the values returned by the Compare method in the previous table.

Important

The String.CompareOrdinal method is primarily intended for use when ordering or sorting strings. You should not use the String.CompareOrdinal method to test for equality (that is, to explicitly look for a return value of 0 with no regard for whether one string is less than or greater than the other). Instead, to determine whether two strings are equal, use the String.Equals(String, String, StringComparison) method.

The following example uses the CompareOrdinal method to compare the values of two strings.

String^ string1 = "Hello World!";
Console::WriteLine(String::CompareOrdinal(string1, "hello world!"));
string string1 = "Hello World!";
Console.WriteLine(String.CompareOrdinal(string1, "hello world!"));
Dim string1 As String = "Hello World!"
Console.WriteLine(String.CompareOrdinal(string1, "hello world!"))

This example displays -32 to the console.

CompareTo method

The String.CompareTo method compares the string that the current string object encapsulates to another string or object. The return values of this method are identical to the values returned by the String.Compare method in the previous table.

Important

The String.CompareTo method is primarily intended for use when ordering or sorting strings. You should not use the String.CompareTo method to test for equality (that is, to explicitly look for a return value of 0 with no regard for whether one string is less than or greater than the other). Instead, to determine whether two strings are equal, use the String.Equals(String, String, StringComparison) method.

The following example uses the String.CompareTo method to compare the string1 object to the string2 object.

String^ string1 = "Hello World";
String^ string2 = "Hello World!";
int MyInt = string1->CompareTo(string2);
Console::WriteLine( MyInt );
string string1 = "Hello World";
string string2 = "Hello World!";
int MyInt = string1.CompareTo(string2);
Console.WriteLine( MyInt );
Dim string1 As String = "Hello World"
Dim string2 As String = "Hello World!"
Dim MyInt As Integer = string1.CompareTo(string2)
Console.WriteLine(MyInt)

This example displays -1 to the console.

All overloads of the String.CompareTo method perform culture-sensitive and case-sensitive comparisons by default. No overloads of this method are provided that allow you to perform a culture-insensitive comparison. For code clarity, we recommend that you use the String.Compare method instead, specifying CultureInfo.CurrentCulture for culture-sensitive operations or CultureInfo.InvariantCulture for culture-insensitive operations. For examples that demonstrate how to use the String.Compare method to perform both culture-sensitive and culture-insensitive comparisons, see Performing Culture-Insensitive String Comparisons.

Equals method

The String.Equals method can easily determine if two strings are the same. This case-sensitive method returns a true or false Boolean value. It can be used from an existing class, as illustrated in the next example. The following example uses the Equals method to determine whether a string object contains the phrase "Hello World".

String^ string1 = "Hello World";
Console::WriteLine(string1->Equals("Hello World"));
string string1 = "Hello World";
Console.WriteLine(string1.Equals("Hello World"));
Dim string1 As String = "Hello World"
Console.WriteLine(string1.Equals("Hello World"))

This example displays True to the console.

This method can also be used as a static method. The following example compares two string objects using a static method.

String^ string1 = "Hello World";
String^ string2 = "Hello World";
Console::WriteLine(String::Equals(string1, string2));
string string1 = "Hello World";
string string2 = "Hello World";
Console.WriteLine(String.Equals(string1, string2));
Dim string1 As String = "Hello World"
Dim string2 As String = "Hello World"
Console.WriteLine(String.Equals(string1, string2))

This example displays True to the console.

StartsWith and EndsWith methods

You can use the String.StartsWith method to determine whether a string object begins with the same characters that encompass another string. This case-sensitive method returns true if the current string object begins with the passed string and false if it does not. The following example uses this method to determine if a string object begins with "Hello".

String^ string1 = "Hello World";
Console::WriteLine(string1->StartsWith("Hello"));
string string1 = "Hello World";
Console.WriteLine(string1.StartsWith("Hello"));
Dim string1 As String = "Hello World!"
Console.WriteLine(string1.StartsWith("Hello"))

This example displays True to the console.

The String.EndsWith method compares a passed string to the characters that exist at the end of the current string object. It also returns a Boolean value. The following example checks the end of a string using the EndsWith method.

String^ string1 = "Hello World";
Console::WriteLine(string1->EndsWith("Hello"));
string string1 = "Hello World";
Console.WriteLine(string1.EndsWith("Hello"));
Dim string1 As String = "Hello World!"
Console.WriteLine(string1.EndsWith("Hello"))

This example displays False to the console.

IndexOf and LastIndexOf methods

You can use the String.IndexOf method to determine the position of the first occurrence of a particular character within a string. This case-sensitive method starts counting from the beginning of a string and returns the position of a passed character using a zero-based index. If the character cannot be found, a value of –1 is returned.

The following example uses the IndexOf method to search for the first occurrence of the 'l' character in a string.

String^ string1 = "Hello World";
Console::WriteLine(string1->IndexOf('l'));
string string1 = "Hello World";
Console.WriteLine(string1.IndexOf('l'));
Dim string1 As String = "Hello World!"
Console.WriteLine(string1.IndexOf("l"))

This example displays 2 to the console.

The String.LastIndexOf method is similar to the String.IndexOf method except that it returns the position of the last occurrence of a particular character within a string. It is case-sensitive and uses a zero-based index.

The following example uses the LastIndexOf method to search for the last occurrence of the 'l' character in a string.

String^ string1 = "Hello World";
Console::WriteLine(string1->LastIndexOf('l'));
string string1 = "Hello World";
Console.WriteLine(string1.LastIndexOf('l'));
Dim string1 As String = "Hello World!"
Console.WriteLine(string1.LastIndexOf("l"))

This example displays 9 to the console.

Both methods are useful when used in conjunction with the String.Remove method. You can use either the IndexOf or LastIndexOf methods to retrieve the position of a character, and then supply that position to the Remove method in order to remove a character or a word that begins with that character.

See also