1

I'm new to C# I came from a Java background, I'm trying to find a method to compare 2 strings if they are equal but to the ignore case. In Java you can type somthing like this

boolean equal = "abc".equalsIgnoreCase("ABC");  

is there anything like that in C# ?. I know that I can use

var equal = String.Compare("abc", "ABC", StringComparison.OrdinalIgnoreCase);

or

var equal = String.Equals("abc", "ABC", StringComparison.InvariantCultureIgnoreCase);

I just want to know if there is anything shorter (without having to pass a StringComparison Enum )

BluePositive
  • 21
  • 1
  • 9
  • `String.Equals(str1,str2, StringComparison.InvariantCultureIgnoreCase);` – Habib Jul 18 '19 at 19:09
  • 2
    `"abc".Equals("ABC", StringComparison.InvariantCultureIgnoreCase)` – Sohaib Jundi Jul 18 '19 at 19:09
  • @Habib did you read the question? "I just want to know if there is anything shorter". This is not a duplicate. Also your duplicate is itself a duplicate of [this question](https://stackoverflow.com/questions/3694863/c-confusion-about-toupper-and-tolower). – djv Jul 18 '19 at 19:14
  • 4
    @BluePositive The answer to your question is No, there is no such method in the framework. But you can create an extension method which will behave the same way as your Java: `static class ExtensionMethods { public static bool EqualsIgnoreCase(this string source, string value) => source.Equals(value, StringComparison.OrdinalIgnoreCase); }` – djv Jul 18 '19 at 19:20
  • 1
    Thanks, @djv for understanding my question and for the answer – BluePositive Jul 18 '19 at 19:25
  • `var equal = "abc".Equals("ABC", StringComparison.OrdinalIgnoreCase);` is shorter than the sample using `String.Compare` by 9 characters, and it returns a `bool` instead of an `int` (which is useful), so I think the duplicate is correct. :D – Rufus L Jul 18 '19 at 19:28
  • You could also do `var equals = "abc".ToUpper().Equals("ABC".ToUpper());`, which is still shorter and only uses one parameter (not sure why that's an issue). But it's not going to be as accurate for all cultures, and the two `ToUpper` calls will be more expensive. Probably writing an extension method is your best bet if you're using it a lot. – Rufus L Jul 18 '19 at 19:36
  • Is there some expectation that C#/.NET maintains parity with Java as far as the methods it provides and the convenience/brevity with which they can be called? Or is the thought that calling `String.Equals()` to determine case-insensitive equality is so verbose for no particular reason? Because, otherwise, I'm not really seeing the premise or value of this question. If such a "shortcut" method did exist, anyways, wouldn't one expect to find it in the `String` class [right along side](https://learn.microsoft.com/dotnet/api/system.string#methods) of `Equals()`? – Lance U. Matthews Jul 18 '19 at 20:32
  • @BACON no it's not that I thought that C#/.NET has anything with Java. it's just that I want to know if there is a method to check for equals Ignore Case without having to pass a StringComparison Enum – BluePositive Jul 19 '19 at 12:36

1 Answers1

6

you can create an extension method:

internal static class StringExtensions
{
   public static bool EqualsIgnoreCase(this string str1, string str2)
   {
       return String.Equals(str1, str2, StringComparison.InvariantCultureIgnoreCase);
   }
}

Then use it this way: bool equals = "abc".EqualsIgnoreCase("ABC");

DoronG
  • 2,576
  • 16
  • 22