Home
Map
String Compare and CompareToUse the Compare and CompareOrdinal methods on strings. See the results of CompareTo.
C#
This page was last reviewed on Aug 24, 2023.
Compare. The Compare method determines the sort order of strings. It checks if one string is ordered before another when in alphabetical order, whether it is ordered after, or is equivalent.
String Equals
Method info. There are 3 methods that provide this functionality—Compare, CompareOrdinal and CompareTo. Two strings are always compared.
An example. We need the Compare methods for sorting algorithms. We use them to test whether one string comes before or after the other in sorting.
Sort
Part 1 We use the string.Compare static method. It is not called on an instance, and we must pass 2 arguments.
Part 2 We invoke the static CompareOrdinal—this treats strings by their numeric (ordinal) character values.
Part 3 We call the CompareTo instance method. We cannot use this method on a null variable.
CompareTo
Result If the first string is bigger, the result is 1. If the first string is smaller, the result is -1.
And If both strings are equal, the result is 0. The number essentially indicates how much "larger" the first string is.
using System; string a = "a"; string b = "b"; // Part 1: use static Compare. int c = string.Compare(a, b); Console.WriteLine(c); // Part 2: use static CompareOrdinal. c = string.CompareOrdinal(b, a); Console.WriteLine(c); // Part 3: use CompareTo. c = a.CompareTo(b); Console.WriteLine(c); c = b.CompareTo(a); Console.WriteLine(c);
-1 (This means a is smaller than b) 1 (This means b is smaller than a) -1 1
Review, results. It is important to understand the possible results of Compare methods. Here are the int return values—we see just 3 values: -1, zero, and 1.
int, uint
String A: First alphabetically String B: Second alphabetically Compare(A, B): -1 Compare(B, A): 1 Compare(A, A): 0 Compare(B, B): 0
Notes, globalization. By default, string.Compare and CompareTo use the system globalization for safe comparisons. Cultures such as Turkish have common letters that need globalization.
Tip You can specify those cultures in the method call as a parameter to accomplish this goal.
A discussion. All sorting algorithms must see if one string should be ordered before any other string. The Compare methods are ideal for when you need to implement your own sorting methods.
Note Compare methods are not needed to check string equality. You can use the Equals method, or the == operator, for equality.
Case. String comparisons are by default case-sensitive. But you can use the StringComparison.OrdinalIgnoreCase enumerated constant to use a case-insensitive comparison.
StringComparison
Tip This option is useful in many programs, such as those that deal with the Windows file system where file case is not important.
A summary. The Compare methods see if one string is larger, smaller or equal to another. Compare returns 1, 0 or -1 if a string is alphabetically first, equal, or second.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 24, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.