-2

Pretty much what the title says, I got a big if-sentence with ~5Contains this is not nicely looking if I repeat this a few times, is there any way to shorten this?

if(word.Contains("tree") ||word.Contains("water")||word.Contains("sky")||word.Contains("lake")||word.Contains("plant"))
{
//do something
}
wouter
  • 359
  • 4
  • 15
  • `if (new[] { "tree", "water", ... }.Any(s => word.Contains(s)) { ... }`. Use the search. – CodeCaster Jun 18 '15 at 13:05
  • http://stackoverflow.com/questions/10419106/checking-multiple-contains-on-one-string – CodeCaster Jun 18 '15 at 13:06
  • 1
    I like how 5 identical answers were posted. – mclark1129 Jun 18 '15 at 13:07
  • @Mike I don't like that, it means to me those users value their reputation more than a clean site. I may be wrong though. – CodeCaster Jun 18 '15 at 13:08
  • 2
    Well all those answers happened inside a few seconds, so I doubt it's that. – LInsoDeTeh Jun 18 '15 at 13:08
  • 1
    @LInsoDeTeh I think he means answering versus closing as dupe not that they copied each other – Ňɏssa Pøngjǣrdenlarp Jun 18 '15 at 13:10
  • @Plutonix, I agree, but not everyone is here since many years and knows every single post. – LInsoDeTeh Jun 18 '15 at 13:12
  • 1
    I already tried to google my question, but i didnt know actually what my problem was called, so thats why i did ask it on here, anyway if you want you may delete my question :) – wouter Jun 18 '15 at 13:13
  • @wouter - I tried [c# string contains words](https://www.google.com/search?q=c%23+string+contains+words). First try. Just search for what you need to do. – Kobi Jun 18 '15 at 13:17
  • @Kobi I am sorry, I will google a little bit better next time. Should I delete my post now? – wouter Jun 18 '15 at 13:19
  • @wouter - I don't think you *can* delete it. It's OK to have duplicates, so you can just leave it. Thanks! – Kobi Jun 18 '15 at 13:22
  • There's no problem in duplicate questions, as it leads to multiple wordings of the same problem. Users of 2K+ rep should know better though than to repeat existing answers. – CodeCaster Jun 18 '15 at 13:22

7 Answers7

4

Just define an array with the terms and use a LINQ expression:

string[] searchTerms = { "tree", "water", ...}

if (searchTerms.Any(p => word.Contains(p)) {
  // do something
}
LInsoDeTeh
  • 1,028
  • 5
  • 11
1

You can do this:

if (new [] { "tree", "water", "sky", "lake", "plant", }.Any(w => word.Contains(w)))
{
//do something
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

Yes, but the keywords in an array, and switch it round

var words = new[]{"tree","water","sky","lake"};
if(words.Any(w => word.Contains(w))){
  // do something
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • @wouter Your appreciation is appreciated, but it's not necessary to respond to every individual answer. An upvote is more than enough thanks! – mclark1129 Jun 18 '15 at 13:12
  • @MikeC - Speak for yourself. I like the personal touch ;) – Jamiec Jun 18 '15 at 13:13
  • @Jamiec I guess I'll just have to resort to posting my own answer and flagging the thank you for moderator attention! :) – mclark1129 Jun 18 '15 at 13:14
  • 2
    @MikeC - Thank you for responding to me comment about the validity of responses to an answer. See, there's no way this perpetual thanking will get out of hand :D – Jamiec Jun 18 '15 at 13:15
  • @MikeC Do I break a rule with thanking people on their posts? I kinda see that its a problem because I dont talk about the code itself. Is so, I wont do it in the future : – wouter Jun 18 '15 at 13:17
  • @wouter See http://meta.stackexchange.com/questions/126180/is-it-acceptable-to-write-a-thank-you-in-a-comment. You're not breaking any rules or getting into trouble. Thank yous and good manners ARE definitely appreciated, but in general comments are most useful when they serve to improve the question or answer at hand. E.G: "Thanks! I could use a little more information on ..." Anyways, I've said enough. If I'm not careful we'll ALL be breaking the rule about extended chat in the comments! :) – mclark1129 Jun 18 '15 at 13:24
  • @MikeC Thank you mike, will keep an eye on this – wouter Jun 18 '15 at 13:25
1

You can use regex for this:

if (Regex.IsMatch(word, "tree|water|sky|lake|plant")) {
    // Do something
}

This approach is more flexible than using Contains, because it lets you find whole-word matches (i.e. match "tree" but not "subtree", or "plant" but not "implant")

if (Regex.IsMatch(word, @"\b(tree|water|sky|lake|plant)\b")) {
    // Do something
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

There are probably a dozen or more ways you could code this. I'm not really sure I would worry about making it shorter code, so much as I would worry about making it maintainable and readable. Something like this, perhaps:

var wordsToCheck = new[] { "tree", "water", "sky", "lake" };
if (wordsToCheck.Any(wordToCheck => word.Contains(wordToCheck)))
{

}
Tim
  • 14,999
  • 1
  • 45
  • 68
1

looks like others beat me. But you can make it very short using a method group.

var strs = new[] { "tree", "water", "sky", "lake", "plant" };

if(strs.Any(word.Contains))
{

}
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
1

You can use split then Any

if("tree,water,sky,lake,plant".Split(',').ToList().Any(e=>word.Contains(e)))
Bellash
  • 7,560
  • 6
  • 53
  • 86