Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Compare two strings lexicographically in C#
The String.Compare() method in C# performs lexicographic (alphabetical) comparison between two strings. It compares strings character by character based on their Unicode values and is case-sensitive by default.
Syntax
Following is the syntax for the basic String.Compare() method −
int result = string.Compare(string1, string2);
For case-insensitive comparison −
int result = string.Compare(string1, string2, true);
Return Value
The String.Compare() method returns an integer value indicating the lexicographic relationship between the two strings −
If str1 is less than str2, it returns -1. If str1 is equal to str2, it returns 0. If str1 is greater than str2, it returns 1.
Using Case-Sensitive Comparison
Example
using System;
class Program {
static void Main(string[] args) {
string string1 = "amit";
string string2 = "Amit";
int result = string.Compare(string1, string2);
Console.WriteLine("Comparing '" + string1 + "' with '" + string2 + "'");
Console.WriteLine("Result: " + result);
if (result < 0)
Console.WriteLine("'" + string1 + "' comes before '" + string2 + "'");
else if (result > 0)
Console.WriteLine("'" + string1 + "' comes after '" + string2 + "'");
else
Console.WriteLine("Both strings are equal");
}
}
The output of the above code is −
Comparing 'amit' with 'Amit' Result: 1 'amit' comes after 'Amit'
Using Case-Insensitive Comparison
Example
using System;
class Program {
static void Main(string[] args) {
string string1 = "apple";
string string2 = "BANANA";
int caseSensitive = string.Compare(string1, string2);
int caseInsensitive = string.Compare(string1, string2, true);
Console.WriteLine("Case-sensitive comparison: " + caseSensitive);
Console.WriteLine("Case-insensitive comparison: " + caseInsensitive);
// Multiple string comparisons
string[] words = {"zebra", "apple", "Banana", "cherry"};
Array.Sort(words, string.Compare);
Console.WriteLine("Sorted array:");
foreach (string word in words) {
Console.WriteLine(word);
}
}
}
The output of the above code is −
Case-sensitive comparison: -1 Case-insensitive comparison: -1 Sorted array: Banana apple cherry zebra
Comparison with StringComparison Enum
Example
using System;
class Program {
static void Main(string[] args) {
string str1 = "Hello";
string str2 = "hello";
int ordinal = string.Compare(str1, str2, StringComparison.Ordinal);
int ordinalIgnoreCase = string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase);
int currentCulture = string.Compare(str1, str2, StringComparison.CurrentCulture);
Console.WriteLine("Ordinal comparison: " + ordinal);
Console.WriteLine("Ordinal ignore case: " + ordinalIgnoreCase);
Console.WriteLine("Current culture: " + currentCulture);
}
}
The output of the above code is −
Ordinal comparison: -1 Ordinal ignore case: 0 Current culture: -1
Comparison Methods
| Method | Description | Case Sensitive |
|---|---|---|
| string.Compare(str1, str2) | Basic lexicographic comparison | Yes |
| string.Compare(str1, str2, true) | Comparison with case sensitivity control | No (when true) |
| string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase) | Ordinal comparison ignoring case | No |
| str1.CompareTo(str2) | Instance method for comparison | Yes |
Conclusion
The String.Compare() method provides flexible lexicographic string comparison in C#. Use the basic method for case-sensitive comparison, add a boolean parameter for case-insensitive comparison, or use StringComparison enum for more specific comparison requirements.
