In this C# tutorial explains, how to compare two strings character by character in C# using various methods and examples.
Compare Two Strings Character by Character in C#
Let us check out, how to compare two strings character by character in C# using various methods.
Method 1: Using For Loop
The most straightforward way to compare two strings character by character is by using a for
loop in C#. First, check if both strings have the same length. If they do, you can proceed to compare each character one by one.
Here’s how you can do it:
using System;
namespace StringComparisonExample
{
class Program
{
static void Main(string[] args)
{
string name1 = "Alice";
string name2 = "Alicia";
if (name1.Length != name2.Length)
{
Console.WriteLine("The strings have different lengths, hence they are not equal.");
}
else
{
bool areEqual = true;
for (int i = 0; i < name1.Length; i++)
{
if (name1[i] != name2[i])
{
areEqual = false;
Console.WriteLine($"Strings differ at character {i + 1}: {name1[i]} != {name2[i]}");
break;
}
}
if (areEqual)
{
Console.WriteLine("The strings are equal.");
}
}
}
}
}
In this example, the names “Alice” and “Alicia” are compared. Since their lengths are different, the program outputs that they are not equal. You can check out the output like the below screenshot.
Method 2: Using foreach
and IEnumerator
Another way to compare two strings is by using foreach
along with IEnumerator
in C#. This method works well if you want to compare strings without caring about their length differences.
Here’s how you can do it:
using System;
using System.Collections.Generic;
namespace StringComparisonExample
{
class Program
{
static void Main(string[] args)
{
string name1 = "John";
string name2 = "Johnny";
IEnumerator<char> enumerator1 = name1.GetEnumerator();
IEnumerator<char> enumerator2 = name2.GetEnumerator();
int position = 0;
while (enumerator1.MoveNext() && enumerator2.MoveNext())
{
if (enumerator1.Current != enumerator2.Current)
{
Console.WriteLine($"Strings differ at character {position + 1}: {enumerator1.Current} != {enumerator2.Current}");
return;
}
position++;
}
if (enumerator1.MoveNext() || enumerator2.MoveNext())
{
Console.WriteLine("The strings have different lengths, hence they are not equal.");
}
else
{
Console.WriteLine("The strings are equal.");
}
}
}
}
Method 3: Using LINQ
If you’re comfortable with LINQ, you can use it to compare two strings elegantly. This method is concise but might not be as straightforward as the previous ones.
using System;
using System.Linq;
namespace StringComparisonExample
{
class Program
{
static void Main(string[] args)
{
string name1 = "Emily";
string name2 = "Emma";
bool areEqual = name1.Length == name2.Length &&
!name1.Where((t, i) => t != name2[i]).Any();
if (areEqual)
{
Console.WriteLine("The strings are equal.");
}
else
{
Console.WriteLine("The strings are not equal.");
}
}
}
}
In this example, names “Emily” and “Emma” are compared. They are found to be unequal as their lengths are different and their characters don’t match entirely. You can see the output like below in the screenshot.
Conclusion
There are multiple ways to compare strings character by character in C#. In this tutorial, we checked 3 methods to compare strings character by character in C#.
You can check out the following tutorials:
- How to get the first n characters of a string in c#.net
- Compare String Value with String Array in C#
- Compare String with Multiple Values in C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…