-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);
1

4 Answers 4

4

By explicitly specifying it:

int c = string.Compare(a, b, StringComparison.InvariantCultureIgnoreCase)
Sign up to request clarification or add additional context in comments.

2 Comments

You should use OrdinalIgnoreCase or CurrentCultureIgnoreCase depending on your needs. You normally don't want to use the InvariantCultures for comparisons - see msdn.microsoft.com/en-us/library/ms973919
@Manfred Thanks I was thinking about String.Equals(stringA, stringB, StringComparison.CurrentCultureIgnoreCase)
2

Try this code

int c = string.Compare(a, b,true);

1 Comment

the last argument should be true (ignoreCase)
1

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

        string b = "b";
        string a = "";
        int c = string.Compare(a.ToLower(), b.ToLower());

Comments

-2
String.Equals(stringA, stringB, StringComparison.CurrentCultureIgnoreCase)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.