Open In App

How to Compare Strings in C#?

Last Updated : 10 Sep, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

In C#, comparing strings is an operation used to check equality, order or differences between text values. The .NET framework provides multiple ways to compare strings, such as using operators, methods and culture-specific comparisons, making it flexible and precise.

There are some common methods used to compare Strings:

1. Using String.Equals()

  • Compares two strings for equality.
  • Returns a Boolean (true if equal, otherwise false).
  • Available as both static and instance methods.
  • Supports case-sensitive and case-insensitive comparisons

Syntax:

bool result = String.Equals(str1, str2);

Example: 

C#
using System;

class Program
{
    static void Main()
    {
        string str1 = "Hello";
        string str2 = "hello";

        // Case-sensitive comparison
        bool caseSensitive = str1.Equals(str2);
        Console.WriteLine("Case Sensitive: " + caseSensitive);

        // Case-insensitive comparison
        bool caseInsensitive = String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);
        Console.WriteLine("Case Insensitive: " + caseInsensitive);
    }
}

Output
Case Sensitive: False
Case Insensitive: True

2. Using String.Compare()

  • Compares two strings and returns an integer:
  • if smaller than 0, first string is smaller
  • if equal to 0, strings are equal
  • if greater than 0, first string is greater
  • Useful when you need ordering information, not just equality.

Syntax:

int result = String.Compare(str1, str2);

Example:

C#
using System;

class Program
{
    static void Main()
    {
        int result = String.Compare("apple", "banana");

        Console.WriteLine("Comparison Result: " + result);

        if (result < 0)
            Console.WriteLine("\"apple\" comes before \"banana\"");
        else if (result > 0)
            Console.WriteLine("\"apple\" comes after \"banana\"");
        else
            Console.WriteLine("\"apple\" and \"banana\" are equal");
    }
}

Output
Comparison Result: -1
"apple" comes before "banana"

3. Using CompareTo()

  • An instance method of String.
  • Works like String.Compare() but called directly on a string object.

Syntax:

int result = str1.CompareTo(str2);

Example:

C#
using System;

class Program
{
    static void Main()
    {
        string str1 = "cat";
        string str2 = "dog";

        int result = str1.CompareTo(str2);

        Console.WriteLine("Comparison Result: " + result);

        if (result < 0)
            Console.WriteLine("\"" + str1 + "\" comes before \"" + str2 + "\"");
        else if (result > 0)
            Console.WriteLine("\"" + str1 + "\" comes after \"" + str2 + "\"");
        else
            Console.WriteLine("\"" + str1 + "\" and \"" + str2 + "\" are equal");
    }
}

Output
Comparison Result: -1
"cat" comes before "dog"

4. Using StringComparer Class

  • Provides predefined comparers (case-sensitive, case-insensitive, culture-aware).
  • Useful in collections like dictionaries or when you want reusable comparison logic.

Syntax:

StringComparer comparer = StringComparer.OrdinalIgnoreCase;

int result = comparer.Compare(str1, str2);

Predefined StringComparer Instances

1. StringComparer.CurrentCulture

  • Compares strings using the current culture settings.
  • Case-sensitive.
  • Example: "straße" and "strasse" may be treated as equal in some cultures.

2. StringComparer.CurrentCultureIgnoreCase

  • Same as above but case-insensitive.
  • Example: "Hello" equals "hello" in the current culture.

3. StringComparer.InvariantCulture

  • Compares using the invariant culture (culture-independent, fixed for consistency).
  • Case-sensitive.
  • Useful when you want predictable results across different system locales.

4. StringComparer.InvariantCultureIgnoreCase

  • Same as above but case-insensitive.
  • Example: "File" equals "file", independent of culture.

5. StringComparer.Ordinal

  • Compares strings based on Unicode (binary) values.
  • Case-sensitive.
  • Fastest and most precise for non-linguistic comparisons (like file paths, keys).

6. StringComparer.OrdinalIgnoreCase

  • Same as StringComparer.Ordinal, but case-insensitive.
  • Commonly used for identifiers, file names or keys where case should be ignored.

Example: Demonstrating all the different predefined StringComparer options

C#
using System;

class Program
{
    static void Main()
    {
        string s1 = "Hello";
        string s2 = "hello";
        string s3 = "straße";
        string s4 = "strasse";

        // 1. CurrentCulture (case-sensitive)
        Console.WriteLine("CurrentCulture: " +
            StringComparer.CurrentCulture.Equals(s3, s4)); 

        // 2. CurrentCultureIgnoreCase
        Console.WriteLine("CurrentCultureIgnoreCase: " +
            StringComparer.CurrentCultureIgnoreCase.Equals(s1, s2));

        // 3. InvariantCulture (case-sensitive)
        Console.WriteLine("InvariantCulture: " +
            StringComparer.InvariantCulture.Equals(s3, s4));

        // 4. InvariantCultureIgnoreCase
        Console.WriteLine("InvariantCultureIgnoreCase: " +
            StringComparer.InvariantCultureIgnoreCase.Equals(s1, s2));

        // 5. Ordinal (case-sensitive)
        Console.WriteLine("Ordinal: " +
            StringComparer.Ordinal.Equals(s1, s2));

        // 6. OrdinalIgnoreCase
        Console.WriteLine("OrdinalIgnoreCase: " +
            StringComparer.OrdinalIgnoreCase.Equals(s1, s2));
    }
}

Output
CurrentCulture: True
CurrentCultureIgnoreCase: True
InvariantCulture: True
InvariantCultureIgnoreCase: True
Ordinal: False
OrdinalIgnoreCase: True

5. Using Equality Operator (==)

  • Compares string values directly.
  • Case-sensitive by default.
  • Simpler syntax, commonly used for equality checks.

Example:

C#
using System;

class Program
{
    static void Main()
    {
        string str1 = "World";
        string str2 = "World";
        string str3 = "world";

        Console.WriteLine(str1 == str2); // true (same content, case-sensitive)
        Console.WriteLine(str1 == str3); // false (different case)
    }
}

Output
True
False

6. Character-by-Character Comparison (Custom Method)

  • Useful when you want full control over comparison logic.
  • Works by comparing each character one by one.

Example:

C#
using System;

class Program
{
    static int CompareStrings(string s1, string s2)
    {
        int len = Math.Min(s1.Length, s2.Length);

        for (int i = 0; i < len; i++)
        {
            if (s1[i] < s2[i]) return -1;
            if (s1[i] > s2[i]) return 1;
        }

        if (s1.Length == s2.Length) return 0;
        return (s1.Length < s2.Length) ? -1 : 1;
    }

    static void Main()
    {
        Console.WriteLine(CompareStrings("abc", "abd"));  
        Console.WriteLine(CompareStrings("abc", "abc"));  
        Console.WriteLine(CompareStrings("abd", "abc"));  
        Console.WriteLine(CompareStrings("abc", "ab"));   
        Console.WriteLine(CompareStrings("ab", "abc"));
    }
}

Output
-1
0
1
1
-1

Explanation:

  1. Find the smaller length of the two strings.
  2. Loop through characters one by one.
  3. If a character in s1 is smaller, return -1 and if larger, return 1.
  4. If all compared characters are equal, check lengths: if same length then return 0, if shorter string then -1 and if longer string then 1.

Note: It compares strings lexicographically, like a manual version of CompareTo().


Explore