-4

Is there a variant of this?

if (blabla.Contains("I'm a noob") | blabla.Contains("sry") | blabla.Contains("I'm a noob "+"sry"))
    {
        //stuff
    }

like:

if (blabla.Contains("I'm a noob) and/or ("sry")
    {
        //stuff
    }

Help is appreciated!

HPaulson
  • 83
  • 10
  • The logical operator `||` (or) in C# will evaluate true if either one _or both_ are true, is this what you mean? – Sam Mar 21 '14 at 18:33
  • yes thanks, I read somewhere that | and || was the same.. – HPaulson Mar 21 '14 at 18:36
  • No, `||` for instance will never evaluate the second part if the first is `true`. – Willem Van Onsem Mar 21 '14 at 18:36
  • Correct @CommuSoft, the `||` is considered a shortcut operator and only evaluates the second part if the first part is false. `|` however, will always evaluate both arguments – Sam Mar 21 '14 at 18:37
  • 1
    Just to clarify since I just responded for Lists and others are talking about Strings, what kind of class is blabla? Is it Enumerable? – Sean Duggan Mar 21 '14 at 18:38
  • Use XOR: `if (blabla.Contains("I'm a noob)") ^ blabla.Contains("sry"))` – Ganesh Mar 21 '14 at 18:41
  • 1
    @Ganesh That's not the desired semantics though. He's ORing all of them, and that produces the desired result. – Servy Mar 21 '14 at 18:41
  • 1
    lol, I'm never getting over 5 rep, and yes I know that I probably should have known that. – HPaulson Mar 21 '14 at 18:44
  • the blabla is actually speech which is e.Result.Text. for a jarvis-like program – HPaulson Mar 21 '14 at 18:46
  • 1
    @user3374534: The reason of the downvotes is probably because you don't specify the qustions correctly. For instance: what is the type of blabla... – Willem Van Onsem Mar 21 '14 at 18:49
  • @CommuSoft Oh, just took an example, instead of going in on details. but thx – HPaulson Mar 21 '14 at 18:54

4 Answers4

2

You can't collapse it quite as far as you asked, but you can do:

if (blabla.Contains("I'm a noob") || blabla.Contains("sry"))
{
    //stuff
}

The "and" case is handled here because a string with both would actually pass both of the statements in the "Or".

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • 1
    That's fine if `blabla` is small. However if `blabla` is 100k chars long, you need to run two times over the string. So sometimes this is too much... – Willem Van Onsem Mar 21 '14 at 18:48
  • 1
    Without going down the regex route, you aren't going to avoid that problem. At least mine will shortcut if the first one evaluates to "true". The regex route is more performant (as you noted), but also potentially more confusing, and definitely less readable. – BradleyDotNET Mar 21 '14 at 18:50
  • True, this is a good solution, but like all solutions, you need to know something about the size of the objects involved :D – Willem Van Onsem Mar 21 '14 at 18:51
1

As far as I'm aware, there are no built-in methods to do this. But with a little LINQ and extension methods, you can create your own methods that will check to see if a string contains any or all tokens:

public static class ExtensionMethods{
    public static bool ContainsAny(this string s, params string[] tokens){
        return tokens.Any(t => s.Contains(t));
    }

    public static bool ContainsAll(this string s, params string[] tokens){
        return tokens.All(t => s.Contains(t));
    }
}

You could use it like this (remember, params arrays take a variable number of parameters, so you're not limited to just two like in my example):

var str = "this is a string";
Console.WriteLine(str.ContainsAny("this", "fake"));
Console.WriteLine(str.ContainsAny("doesn't", "exist"));
Console.WriteLine(str.ContainsAll("this", "is"));
Console.WriteLine(str.ContainsAll("this", "fake"));

Output:

True
False
True
False

Edit:

For the record, LINQ is not necessary. You could just as easily write them this way:

public static class ExtensionMethods{
    public static bool ContainsAny(this string s, params string[] tokens){
        foreach(string token in tokens)
            if(s.Contains(token)) return true;
        return false;
    }

    public static bool ContainsAll(this string s, params string[] tokens){
        foreach(string token in tokens)
            if(!s.Contains(token)) return false;
        return true;
    }
}
Curtis Rutland
  • 776
  • 4
  • 12
1
var arr = new[]{"I'm a noob" ,"sry", "I'm a noob +sry"};
if(arr.Any(x => blabla.Contains(x)))
{

}
L.B
  • 114,136
  • 19
  • 178
  • 224
0

You can use a regex:

Regex r = new Regex("I'm a noob|sry|I'm a noob sry");
if(r.IsMatch(blabla)) {
    //TODO: do something
}

Regular expressions have other advanced features like: a* matches with the empty string, a, aa, aaa,...

The funny part is that if you "compile" the regex (for instance using new Regex("I'm a noob|sry|I'm a noob sry",RegexOptions.Compiled), C# will turn it automatically into the fastest solution mechanism possible. For instance if blabla is a 100k chars string, you will only run once over the entire string. And for instance redundant parts like I'm a noob sry will be omitted automatically.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555