tags:

views:

133

answers:

3

I want to check whether a string matches one of a list of values.

There are of course many, many ways I could go about this: if statement, switch statement, RegEx, etc. However, I would have thought that .Net would have something along the lines of

if (myString.InList("this", "that", "the other thing"))

So far the closest thing I can find would be something like:

"this; that; the other thing;".Contains(myString)

Is that pretty much the only way to go if I want to do the check in a single line and don't want to use RegEx?

+9  A: 

If you're using .NET 3.0, there's a method of type enumarable objects that would allow this to work on a string array constructed in-line. Does this work for you?

if ((new string[] { "this", "that", "the other thing" }).Contains(myString))

Thanks for the tip comment. Indeed, this also works:

if ((new [] { "this", "that", "the other thing" }).Contains(myString))

I've been ambivalent about whether using inferred types is a good idea. The brevity is nice but other times I get frustrated at trying to figure out the data types of certain variables when the type isn't explicitly stated. Certainly for something as simple as this, though, the type should be obvious.

BlueMonkMN
You can even skip the "string" part of it as it can be inferred.
Brian Rasmussen
The compiler/Intellisense figures it out and the tooltips or other information (the array type should be displayed in the info for Contains(), for example) will display it. (If the compiler can't figure out the type either, it won't compile. Inferred types in C# are resolved at compilation, not runtime.)
Zooba
Even though they are resolved at compile time, there's no way to easily copy the type name to make another variable of the same type.
BlueMonkMN
Thanks, but I'm confused on this answer. Why is the string array necessary and/or preferable as opposed to the string literal (as described in the original posting)? In other words, if "abc def ghi".Contains(x)works fine, then why use the more verbose (new [] { "abc", "def", "ghi" }).Contains(x)?Anyway, I appreciate the input! I accepted the answer by Josep Medved, as it is the closest to what I was looking for.
Rob3C
The method of using the string literal in the original posting would give a false positive if you ask it to match "thing" will it not? "thing" occurs in the list string, but you don't want it to match unless it is preceded by "the other" do you?
BlueMonkMN
Do string arrays have Contains method? I'm not so sure. Although List<string> does have it.
Josip Medved
No, string arrays do not have a Contains method. Contains is an extension method on anything that implements IEnumerable<>. It's a method of IEnumerable<>.
BlueMonkMN
@BlueMonkMN: You are correct about the false positive. That's what I was getting at with the ";" - for it to work right it really has to be called like ";abc;def;ghi;".Contains(";" + searchString + ";") which is admittedly a bit ridiculous.
Rob3C
+3  A: 

You could use extension methods available in .NET 3.5 to have syntax like

if (myString.InList("this", "that", "the other thing")) {}

Just add something like this (and import it):

public static bool InList(this string text, params string[] listToSearch) {
    foreach (var item in listToSearch) {
        if (string.Compare(item, text, StringComparison.CurrentCulture) == 0) {
            return true;
        }
    }
    return false;
}

If you are using older version, you could still use this function but you will need to call it like:

if (InList(myString, "this", "that", "the other thing")) {}

of course, in InList function, remove this keyword.

Josip Medved
Thanks, this is the closest to what I was looking for. Unfortunately I'm not able to use 3.5 right now, but this is very nice information to know about.
Rob3C
+1  A: 

You could combine yours and BlueMonks answer and use (assuming .NET 3):

if ("this;that;the other thing;".Split(";").Contains(myString) ...
Dan Diplo