c# - String equals case insensitive

Check whether two Strings are equal by case-insensitive comparison
The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects.

The following .net c# tutorial code demonstrates how we can check whether two String instances are equal when we ignore the case while comparing the String instances. So in this .net c# tutorial code example, we will check whether two String objects have the same value or not in a case-insensitive comparison way. The .net c# developers can check the two String object’s equality by using the String Equals() method.

The String Equals() method determines whether two String objects have the same value. The String Equals(String, StringComparison) method overload determines whether this String and a specified String object have the same value. The second parameter specifies the culture, case, and sort rules used in the comparison. So using this method overload we can compare two String instance equality by ignoring the case.

The String Equals(string? value, StringComparison comparisonType) method overload has two parameters. The value parameter is the String to compare to this instance. And the comparisonType parameter is one of the enumeration values that specifies how the Strings will be compared. Here we passed StringComparison.OrdinalIgnoreCase value for the comparisonType parameter to ignore the case while comparing two String instances.

The String Equals(String, StringComparison) method returns a Boolean value. It returns true if the value of the value parameter is the same as this String otherwise the method returns false. The method overload throws ArgumentException if the comparisonType parameter value is not a StringComparison value.

Finally, we can check whether two String instances are equal or not while comparing them in a case-insensitive way by using the String Equals(String, StringComparison) method overload.
string-equals-case-insensitive.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        //this section create string variables.
        string stringPlants = "Cutleaf Coneflower";
        string stringPlants2 = "CUTLEAF Coneflower";
        string stringPlants3 = "Golden Corydalis";

        Label1.Text = "string of plants..................<br />";
        Label1.Text += "stringPlants: " + stringPlants;
        Label1.Text += "<br />stringPlants2: " + stringPlants2;
        Label1.Text += "<br />stringPlants3: " + stringPlants3;

        //this line test two string equality.
        Boolean result1 = stringPlants.Equals(stringPlants2);

        //this line test two string equality ignore case.
        Boolean result1IgnoreCase = stringPlants.Equals(stringPlants2, StringComparison.OrdinalIgnoreCase);

        Boolean result2 = stringPlants.Equals(stringPlants3);

        Label1.Text += "<br /><br />stringPlants equal to stringPlants2...........?" + result1;
        Label1.Text += "<br /><br />stringPlants equal to stringPlants2(ignore case)...........?" + result1IgnoreCase;
        Label1.Text += "<br /><br />stringPlants equal to stringPlants3...........?" + result2;
    }  
</script>  

<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string equals case insensitive</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string equals case insensitive
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="string equals case insensitive"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>