-2

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;
         }
     }
}
A arancibia
  • 281
  • 4
  • 5
  • 19
  • 1
    Please always *first* google your question title before you ask for help. – Hans Passant Nov 11 '15 at 15:25
  • look answer below or use the `.ToLower()` Method of the string class. but in this case the answer below should be better – Jens Nov 11 '15 at 15:25
  • @JensHorstmann -- Your comment is wrong considering the strings being compared against are in uppercase. – rory.ap Nov 11 '15 at 15:27
  • @roryap then he should use `.ToUpper()` – Jens Nov 11 '15 at 15:28
  • I did google it and I was not find it anything concrete. @roryap, thanks for the reply, your solution worked! – A arancibia Nov 11 '15 at 15:29
  • @Aarancibia -- It's always better to demonstrate that you researched even if you couldn't come up with anything. Provide an indication that you searched for something, what you *did* find that didn't match your question, or what it was you were hoping to find. Thus, you'll avoid down votes. – rory.ap Nov 11 '15 at 15:30
  • @roryap you're right I should include more comments about my researches. I will do it next time. Thanks again – A arancibia Nov 11 '15 at 15:32

1 Answers1

3

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.

rory.ap
  • 34,009
  • 10
  • 83
  • 174