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
How to Initialize and Compare Strings in C#?
String initialization and comparison are fundamental operations in C# programming. C# provides multiple ways to initialize strings and several methods to compare them effectively.
String Initialization
There are several ways to initialize a string in C# −
// Direct assignment
string str1 = "Hello, World!";
// Using string constructor
string str2 = new string("Welcome");
// Empty string initialization
string str3 = "";
string str4 = string.Empty;
Example
using System;
class Program {
static void Main(string[] args) {
string name = "Amit";
string message = "Hello, World!";
string empty = string.Empty;
Console.WriteLine("Name: " + name);
Console.WriteLine("Message: " + message);
Console.WriteLine("Empty string length: " + empty.Length);
}
}
The output of the above code is −
Name: Amit Message: Hello, World! Empty string length: 0
String Comparison Methods
C# provides several methods to compare strings. The most commonly used method is String.Compare() −
public static int Compare(string str1, string str2)
The String.Compare() method returns −
0 if both strings are equal
Less than 0 if the first string comes before the second in sort order
Greater than 0 if the first string comes after the second in sort order
Using String.Compare() Method
Example
using System;
class Program {
static void Main(string[] args) {
string str1 = "John";
string str2 = "Andy";
string str3 = "John";
// Compare str1 and str2
int result1 = String.Compare(str1, str2);
Console.WriteLine("Compare("" + str1 + "", "" + str2 + ""): " + result1);
// Compare str1 and str3
int result2 = String.Compare(str1, str3);
Console.WriteLine("Compare("" + str1 + "", "" + str3 + ""): " + result2);
// Check equality
if (String.Compare(str1, str2) == 0) {
Console.WriteLine(str1 + " and " + str2 + " are equal strings.");
} else {
Console.WriteLine(str1 + " and " + str2 + " are not equal strings.");
}
if (String.Compare(str1, str3) == 0) {
Console.WriteLine(str1 + " and " + str3 + " are equal strings.");
} else {
Console.WriteLine(str1 + " and " + str3 + " are not equal strings.");
}
}
}
The output of the above code is −
Compare("John", "Andy"): 1
Compare("John", "John"): 0
John and Andy are not equal strings.
John and John are equal strings.
Alternative String Comparison Methods
C# also provides other methods for string comparison −
Using Equals() Method
using System;
class Program {
static void Main(string[] args) {
string str1 = "Hello";
string str2 = "hello";
string str3 = "Hello";
// Case-sensitive comparison
Console.WriteLine("str1.Equals(str2): " + str1.Equals(str2));
Console.WriteLine("str1.Equals(str3): " + str1.Equals(str3));
// Case-insensitive comparison
Console.WriteLine("Case-insensitive comparison:");
Console.WriteLine("str1.Equals(str2, StringComparison.OrdinalIgnoreCase): " +
str1.Equals(str2, StringComparison.OrdinalIgnoreCase));
}
}
The output of the above code is −
str1.Equals(str2): False str1.Equals(str3): True Case-insensitive comparison: str1.Equals(str2, StringComparison.OrdinalIgnoreCase): True
Comparison Methods Summary
| Method | Return Type | Description |
|---|---|---|
| String.Compare() | int | Returns -1, 0, or 1 based on lexicographic order |
| String.Equals() | bool | Returns true if strings are identical |
| == operator | bool | Returns true if strings have same value |
Conclusion
String initialization in C# is straightforward using direct assignment or constructors. For comparison, String.Compare() provides lexicographic ordering while Equals() checks for exact equality. Choose the appropriate method based on whether you need ordering information or simple equality checking.
