String comparison is a fundamental operation in any programming language, it plays a vital role while sorting data, validating user input, or searching for specific values. In this blog post, we’ll break down the String.Compare() method and its usage and demonstrate how it can simplify string comparison in our C# applications.
The Basics of String.Compare()
The String.Compare() method in C# allows you to compare two strings and obtain a result that indicates their relative order. Let’s start with the basic syntax:
|
1 |
int result = string.Compare(string1, string2); |
Here’s what each part does:
- string1 and string2 are the two strings you want to compare.
-
result is an integer that provides information about how the strings relate to each other:
- If result is negative, string1 comes before string2.
- If result is positive, string1 comes after string2.
- If result is zero, the strings are considered equal.
Case-Sensitive vs. Case-Insensitive Comparison
String.Compare() performs a case-sensitive comparison by default, which means it differentiates between uppercase and lowercase characters. In that case if you compare ‘apple’ and ‘Apple’ then the output value will be negative because ‘a’ comes before ‘A’.
If you need to perform a case-insensitive comparison where ‘a’ and ‘A’ are considered the same, you can use StringComparison.OrdinalIgnoreCase to achieve this:
Let’s check the example below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StringCompareSample { internal class Program { static void Main(string[] args) { string apple1 = "apple"; string apple2 = "Apple"; Console.WriteLine("Default String Comparison."); Console.WriteLine("**********************************************************************"); int comparisonResult = string.Compare(apple1, apple2); Console.WriteLine("String.Compare between 'apple' and 'Apple' : " + comparisonResult); Console.WriteLine("The value of 'comparisonResult' is negative because 'a' comes before 'A'."); Console.WriteLine("\b"); Console.WriteLine("Default String Comparison using StringComparison.OrdinalIgnoreCase."); Console.WriteLine("**********************************************************************"); int comparisonResultIgnoreCase = string.Compare(apple1, apple2, StringComparison.OrdinalIgnoreCase); Console.WriteLine("String.Compare between 'apple' and 'Apple' using ignore case sensitivity : " + comparisonResult); Console.WriteLine("The value of 'comparisonResultIgnoreCase' is zero because we're ignoring case, hence both string are same."); Console.Read(); } } } |
It generates below output.

Sorting Strings
One of the common use cases for String.Compare() is sorting strings. You can use this method in sorting algorithms or when you want to order data in a specific way. Here’s a simple example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StringCompareSample { internal class Program { static void Main(string[] args) { string[] somefruits = { "orange", "apple", "strawberry", "watermelon", "pineapple", "banana" }; Console.WriteLine("String Sorting using string.Compare() method."); Console.WriteLine("************************************************"); Console.WriteLine("Order of Fruits before Sorting."); foreach(string f in somefruits) Console.WriteLine(f); //Sorting array using string.Compare() Array.Sort(somefruits, (a, b) => string.Compare(a, b, StringComparison.OrdinalIgnoreCase)); Console.WriteLine("\b"); Console.WriteLine("Order of Fruits after Sorting."); foreach (string f in somefruits) Console.WriteLine(f); Console.Read(); } } } |
After executing this code, the somefruits array will be sorted alphabetically, ignoring case.

Practical Use of String.Compare()
String.Compare can be handy in some of below use cases.
1. Sorting and Organizing Data: When you need to sort a collection of string data, String.Compare() provides a straightforward solution. You can customize sorting rules by implementing different StringComparison options.
2. Case-Insensitive Search: In search functionality, you might want to conduct a case-insensitive search to ensure that you retrieve results regardless of the user’s input. By using String.Compare() with the appropriate option, you can easily achieve this.
3. Multilingual Support: String.Compare() easily handles multilingual comparisons, to ensure that your application functions correctly across multiple linguistic contexts.
4. User Input Validation: You can use String.Compare() to validate and match the expected values efficiently, for example, user inputs like Email ID, Username, Phone Number, etc.
Conclusion
The String.Compare() method offers a variety of options to perform string-related operations like sorting data, string equality checking, or implementing custom comparison logic, etc, this certainly enhances the functionality of your C# projects and simplifies your coding journey.
Thanks for checking this out 🙂 please visit some of the other string-related blogs.
Usage of String.Split() method in C#
String Interpolation in C# ($)
