Share via


Comparing Strings

The .NET Framework 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.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

The String.Compare method provides a thorough way of comparing the current string object to another string or object. 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 might be returned by this method.

Value type

Condition

A negative integer

strA is less than strB.

0

strA equals strB.

A positive integer

-or-

1

This instance is greater than value.

-or-

value is a null reference (Nothing in Visual Basic).

Important noteImportant Note:

The Compare method is primarily intended for use when ordering or sorting strings. You should not use the 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 Compare method to determine the relative values of two strings.

Dim MyString As String = "Hello World!"
Console.WriteLine([String].Compare(MyString, "Hello World?"))
string MyString = "Hello World!";      
Console.WriteLine(String.Compare(MyString, "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 Performing Culture-Insensitive String Comparisons.

CompareOrdinal

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 noteImportant Note:

The CompareOrdinal method is primarily intended for use when ordering or sorting strings. You should not use the 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.

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

This example displays -32 to the console.

CompareTo

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 Compare method in the previous table.

Important noteImportant Note:

The CompareTo method is primarily intended for use when ordering or sorting strings. You should not use the 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 CompareTo method to compare the MyString object to the OtherString object.

Dim MyString As String = "Hello World"
Dim OtherString As String = "Hello World!"
Dim MyInt As Integer = MyString.CompareTo(OtherString)
Console.WriteLine(MyInt)
String MyString = "Hello World";
String OtherString = "Hello World!";
int MyInt = MyString.CompareTo(OtherString);    
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, it is recommended 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

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".

Dim MyString As String = "Hello World"
Console.WriteLine(MyString.Equals("Hello World"))
string MyString = "Hello World";
Console.WriteLine(MyString.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.

Dim MyString As String = "Hello World"
Dim YourString As String = "Hello World"
Console.WriteLine(String.Equals(MyString, YourString))
string MyString = "Hello World";
string YourString = "Hello World";
Console.WriteLine(String.Equals(MyString, YourString));

This example displays True to the console.

StartsWith and EndsWith

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".

Dim MyString As String = "Hello World"
Console.WriteLine(MyString.StartsWith("Hello"))
string MyString = "Hello World";
Console.WriteLine(MyString.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.

Dim MyString As String = "Hello World"
Console.WriteLine(MyString.EndsWith("Hello"))
string MyString = "Hello World";    
Console.WriteLine(MyString.EndsWith("Hello"));

This example displays False to the console.

IndexOf and LastIndexOf

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.

Dim MyString As String = "Hello World"
Console.WriteLine(MyString.IndexOf("l"c))
string MyString = "Hello World";
Console.WriteLine(MyString.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.

Dim MyString As String = "Hello World"
Console.WriteLine(MyString.LastIndexOf("l"c))
string MyString = "Hello World";    
Console.WriteLine(MyString.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

Other Resources

Basic String Operations

Performing Culture-Insensitive String Operations