Search code examples
c#comparisoncase-sensitive

Ignoring case sensitive in c#


I have 2 string that I am comparing but I need to avoid problem if I use upper case or lower case.

There's any way to achieve this?

Thanks

Here's my code:

if (userID >= 0 && fnIndex >= 0 && lnIndex >= 0)
{
for (int i = 1; i < userDataId.Length; i++)
    {
     var userData = userDataId[i].List;
         if (userData[fnIndex].ToString() == "FIRSTNAME1" &&
         userData[lnIndex].ToString() == "LASTNAME1")
         {
            userId = userData[userID].ToString();
            break;
         }
     }
}

Solution

  • You can (and should always) do this to compare strings instead of using ==:

    if (userData[fnIndex].ToString().Equals(
        "FIRSTNAME1", StringComparison.CurrentCultureIgnoreCase))
    

    Also, "FIRSTNAME1" and "LASTNAME1" should be made into constants.