-1

Is there a standard String method that can take multiple parameters for exact comparison?

The equivalent in T-SQL would be IN('dog','cat',...)

Eg, if there was such a method called EqualToAny() then the below code should return false

string s="catfish";
if (s.EqualToAny("dog", "cat", "human", "dolphin"))
   return true;
else
   return false;
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
userSteve
  • 1,554
  • 1
  • 22
  • 34
  • Explain "partial match"... If you use `ContainsAny`, it's not, for example, going to return true if the string contains just "huma", only if it contains "human". – rory.ap Feb 16 '17 at 14:46
  • In your example what exactly is `s`? – juharr Feb 16 '17 at 14:47
  • 3
    _so catfish should be false_ makes little sense. Provide clear inputs with desired outputs. – H H Feb 16 '17 at 14:47
  • By "partial match" do you mean that a match must have a word boundary? In other words whitespace, punctuation, start of string, end of string, etc? – rory.ap Feb 16 '17 at 14:49
  • 1
    You mean that instead of `s == "dog" || s == "cat" || s == "human"` you want `new [] { "dog", "cat", "human" }.Contains(s)`? – haim770 Feb 16 '17 at 14:49
  • What's the problem with this: `string[] animals = {"dog", "cat", "human", "dolphin"}; if (animals.Contains(s))) { // ... }` – Tim Schmelter Feb 16 '17 at 14:50
  • 3
    @HenkHolterman The more I read this the more I'm confused by _I want the below code to return true for mammals_ – juharr Feb 16 '17 at 14:50
  • 1
    _but ContainsAny returns true_ isn't very clear either. – H H Feb 16 '17 at 14:53
  • As I said under Dmitry's answer, I think the question is ambiguous. I read it as it should return true for "I have a cat name bob." and false for "I have a catfish named bob." – rory.ap Feb 16 '17 at 14:54
  • 2
    **And why hasn't there been any input from the OP??** Time to close this. – rory.ap Feb 16 '17 at 14:56

2 Answers2

1

You can achieve this using the Linq Any() method.

string input = "catfish";
var mammals = new [] {"dog", "cat", "human", "dolphin"};
var result = mammals.Any(v => v == input);

result will be false for "catfish" and true for "cat".

You can also wrap this into an extension method for string:

public static bool EqualsAny(this string str, IEnumerable<string> validStrings) {
    return validStrings.Any(v => v == str);
}

var result = input.EqualsAny(mammals);

Fiddle

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • 1
    The question is not clear, but it might actually be that the OP wants something more like `mammals.Any(v => input.Contains(v))` but then there seems to be a desire to have "cat" only match if there are word boundaries like "I have a cat", but not for something like "I eat catfish". – juharr Feb 16 '17 at 14:52
  • 2
    It looks like OP is looking for a method on the string object, not on the collection. – Adam V Feb 16 '17 at 14:52
  • Now based on the edit this is correct, but of course you could have just done `mammals.Contains(input)`. – juharr Feb 16 '17 at 15:03
  • Yes, `Contains` will also work if we do not need any fancy string comparison (e.g. `v => v.Equals(str, StringComparison.OrdinalIgnoreCase)`) – Georg Patscheider Feb 16 '17 at 15:06
0

you need EqualsAny ..

    // string extension method 
    public static bool EqualsAny(this string source, params  string[] prms)
    {
        return prms.Contains(source);
    }
levent
  • 3,464
  • 1
  • 12
  • 22
  • Thanks, that's just what I want. I was just hoping there already was a method to do this. – userSteve Feb 16 '17 at 15:03
  • @userSteve -- I want to point out that you have failed miserably to participate in this forum as all of the attempts to clarify your question went unanswered by you. It's very frustrating considering the number of people who were trying to help you which you completely ignored. – rory.ap Feb 16 '17 at 15:04
  • @rory.ap I agree my original question could have been better formed and am very happy there were so many responses so quickly, but it really was a matter of only a few minutes! It's not like days have past! – userSteve Feb 16 '17 at 15:06
  • 2
    @userSteve -- Well, that's how this site operates. If you aren't around to clarify your question, it gets down voted and closed very quickly and then forgotten. I don't recommend posting and then walking away. Only post when you're ready to sit there and respond. – rory.ap Feb 16 '17 at 15:07