-2

What is the best way to compare two string without case sensitive using c#? Am using the below codes.

string b = "b";
int c = string.Compare(a, b);
parthi
  • 734
  • 1
  • 4
  • 13

4 Answers4

4

By explicitly specifying it:

int c = string.Compare(a, b, StringComparison.InvariantCultureIgnoreCase)
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • You should use OrdinalIgnoreCase or CurrentCultureIgnoreCase depending on your needs. You normally don't want to use the InvariantCultures for comparisons - see https://msdn.microsoft.com/en-us/library/ms973919 – Alsty May 10 '16 at 09:53
  • @Manfred Thanks I was thinking about String.Equals(stringA, stringB, StringComparison.CurrentCultureIgnoreCase) – parthi May 10 '16 at 09:53
2

Try this code

int c = string.Compare(a, b,true);
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
1

convert them to lower case then compare, There you go:

        string b = "b";
        string a = "";
        int c = string.Compare(a.ToLower(), b.ToLower());
Obadah Hammoud
  • 572
  • 2
  • 13
-2
String.Equals(stringA, stringB, StringComparison.CurrentCultureIgnoreCase)
parthi
  • 734
  • 1
  • 4
  • 13