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
Check if two String objects have the same value in C#
To check if two String objects have the same value in C#, you can use several methods. The most common approach is using the Equals() method, which performs a case-sensitive comparison of string values.
Syntax
Following are the main syntax forms for comparing string values −
string1.Equals(string2) string.Equals(string1, string2) string1 == string2
Using the Equals() Method
The Equals() method is the recommended way to compare string values. It returns true if both strings have identical content −
Example
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "John";
string str2 = "John";
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("String 2 = " + str2);
Console.WriteLine("String 1 is equal to String 2: {0}", str1.Equals(str2));
}
}
The output of the above code is −
String 1 = John String 2 = John String 1 is equal to String 2: True
Example with Different Values
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "Tom";
string str2 = "Kevin";
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("String 2 = " + str2);
Console.WriteLine("String 1 is equal to String 2: {0}", str1.Equals(str2));
}
}
The output of the above code is −
String 1 = Tom String 2 = Kevin String 1 is equal to String 2: False
Case-Insensitive Comparison
For case-insensitive comparison, use the StringComparison.OrdinalIgnoreCase parameter −
Example
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "HELLO";
string str2 = "hello";
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("String 2 = " + str2);
Console.WriteLine("Case-sensitive comparison: {0}", str1.Equals(str2));
Console.WriteLine("Case-insensitive comparison: {0}",
str1.Equals(str2, StringComparison.OrdinalIgnoreCase));
}
}
The output of the above code is −
String 1 = HELLO String 2 = hello Case-sensitive comparison: False Case-insensitive comparison: True
Using the == Operator
The == operator also compares string values and behaves similarly to Equals() for strings −
Example
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "Programming";
string str2 = "Programming";
string str3 = "Coding";
Console.WriteLine("str1 == str2: {0}", str1 == str2);
Console.WriteLine("str1 == str3: {0}", str1 == str3);
Console.WriteLine("Using Equals(): {0}", str1.Equals(str2));
}
}
The output of the above code is −
str1 == str2: True str1 == str3: False Using Equals(): True
Comparison of String Comparison Methods
| Method | Case Sensitive | Null Handling | Performance |
|---|---|---|---|
| str1.Equals(str2) | Yes | Throws exception if str1 is null | Good |
| string.Equals(str1, str2) | Yes | Handles null values safely | Best |
| str1 == str2 | Yes | Handles null values safely | Good |
Conclusion
To compare string values in C#, use the Equals() method for most scenarios. For null-safe comparisons, prefer string.Equals(str1, str2) or the == operator. Use StringComparison.OrdinalIgnoreCase when case-insensitive comparison is needed.
