C# String Compare() (With Examples) - Programiz
Maybe your like
The Compare() method compares two strings in the alphabetical order.
Example
using System; namespace CsharpString { class Test { public static void Main(string [] args) { string str1 = "C#"; string str2 = "Programiz"; // compares str1 with str2 // returns -1 because C# comes before Programiz in alphabetical order int result = String.Compare(str1, str2); Console.WriteLine(result); Console.ReadLine(); } } } // Output: -1Compare() Syntax
The syntax of the string Compare() method is:
String.Compare(string str1, string str2)Here, Compare() is a method of class String.
Compare() Parameters
The Compare() method takes the following parameters:
- str1 - first string for comparison
- str2 - second string for comparison
Compare() Return Value
The Compare() method returns:
- 0 - if the strings are equal
- positive integer - if the first string comes after the second string in the alphabetical order
- negative integer - if the first string comes before the second string in the alphabetical order
Example 1: C# String Compare()
using System; namespace CsharpString { class Test { public static void Main(string [] args) { string str1 = "C#"; string str2 = "C#"; string str3 = "Programiz"; int result; // compares str1 with str2 result = String.Compare(str1, str2); Console.WriteLine(result); //compares str1 with str3 result = String.Compare(str1, str3); Console.WriteLine(result); //compares str3 with str1 result = String.Compare(str3, str1); Console.WriteLine(result); Console.ReadLine(); } } }Output
0 -1 1Here,
- String.Compare(str1, str2) returns 0 since str1 and str2 are equal
- String.Compare(str1, str3) returns -1 since str1 comes before str3
- String.Compare(str3, str1) returns 1 since str3 comes after str1
Example 2: Check if Two Strings Are Equal
using System; namespace CsharpString { class Test { public static void Main(string [] args) { string str1 = "C#"; string str2 = "C#"; // if str1 and str2 are equal, the result is 0 if(String.Compare(str1, str2) == 0) { Console.WriteLine("str1 and str2 are equal"); } else { Console.WriteLine("str1 and str2 are not equal."); } Console.ReadLine(); } } }Output
str1 and str2 are equalSince, str1 is equal to str2, String.Compare(str1, str2) returns 0.
Example 3: C# String Compare() With Case
using System; namespace CsharpString { class Test { public static void Main(string [] args) { string str1 = "Programiz"; string str2 = "programiz"; int result; // compares str1 and str2 result = String.Compare(str1, str2); Console.WriteLine(result); Console.ReadLine(); } } }Output
1When "Programiz" is compared to "programiz", we do not get 0. It is because the Compare() method considers the letter case.
Example 4: C# String Compare() - Ignore Case
using System; namespace CsharpString { class Test { public static void Main(string [] args) { string str1 = "Programiz"; string str2 = "programiz"; int result; bool ignoreCase = true; // compares by ignoring case result = String.Compare(str1, str2, ignoreCase); Console.WriteLine(result); Console.ReadLine(); } } }Output
0When "Programiz" is compared to "programiz", we get 0. This is because we have the Boolean value true in the method parameter, which ignores the case during string comparison.
Notes:
- Boolean value false considers the letter case during string comparison.
- We can have additional parameters in the String.Compare() method, like bool ignorecase.
Tag » How To Compare String In C#
-
How To Compare Strings - C# Guide - Microsoft Docs
-
Compare Strings In C# - TutorialsTeacher
-
How To Compare Strings In C#
-
C# String Compare() Method - Javatpoint
-
How To Compare Strings In C#? - GeeksforGeeks
-
How To Compare Strings In A Sort Order C#
-
How To Compare Two Strings In C# - TechieClues
-
4 Ways Of C# String Comparison: [Compare, Equals, CompareTo And)
-
Comparing Strings | Manipulating Strings In C# | Peachpit
-
Compare String And Object In C# - Stack Overflow
-
2.7. Controlling Case Sensitivity When Comparing Two Strings
-
How To Use C# String Compare - C# Tutorial And Source Code
-
How String Comparison Works In .NET - C# Video Tutorial - LinkedIn
-
C# String Compare And CompareTo - Dot Net Perls