String contains another two strings

136,390

Solution 1

You would be better off just calling Contains twice or making your own extension method to handle this.

string d = "You hit someone for 50 damage";
string a = "damage";
string b = "someone";
string c = "you";

if(d.Contains(a) && d.Contains(b))
{
   Console.WriteLine(" " + d);
   Console.ReadLine();
}

As far as your other question, you could build a regular expression to parse the string to find 50 or if the string is always the same, just split it based on a space and get the 5th part.

Solution 2

public static class StringExtensions
{
    public static bool Contains(this string s, params string[] predicates)
    {
        return predicates.All(s.Contains);
    }
}

string d = "You hit someone for 50 damage";
string a = "damage";
string b = "someone";
string c = "you";

if (d.Contains(a, b))
{
    Console.WriteLine("d contains a and b");
}

Solution 3

That is because the if statements returns false since d doesn't contain b + a i.e "someonedamage"

Solution 4

Are you looking for the string contains a certain number of words or contains specific words? Your example leads towards the latter.

In that case, you may wish to look into parsing strings or at least use regex.
Learn regex - it will be useful 1000x over in programming. I cannot emphasize this too much. Using contains and if statements will turn into a mess very quickly.

If you are just trying to count words, then :

string d = "You hit someone for 50 damage";  
string[] words = d.Split(' ');  // Break up the string into words
Console.Write(words.Length);  

Solution 5

With the code d.Contains(b + a) you check if "You hit someone for 50 damage" contains "someonedamage". And this (i guess) you don't want.

The + concats the two string of b and a.

You have to check it by

if(d.Contains(b) && d.Contains(a))
Share:
136,390
Winkz
Author by

Winkz

Updated on October 18, 2020

Comments

  • Winkz
    Winkz over 3 years

    Is it possible to have the contain function find if the string contains 2 words or more? This is what I'm trying to do:

    string d = "You hit someone for 50 damage";
    string a = "damage";
    string b = "someone";
    string c = "you";
    
    if(d.Contains(b + a))
    {   
        Console.WriteLine(" " + d);
        Console.ReadLine();
    }
    

    When I run this, the console window just shuts down really fast without showing anything.

    And another question: if I for one want to add how much damage is done, what would be the easiest way to get that number and get it into a TryParse?

  • siride
    siride about 11 years
    The Cast<string>() is unnecessary. You can also combine the Where() and Any(), since Any() can take a filter argument.
  • Winkz
    Winkz about 11 years
    Hmm why if the number isnt at the 5th spot in every line?
  • Brian Dishaw
    Brian Dishaw about 11 years
    If it's not in the same spot, then that won't work. You will want to do a regular expression instead. \d* will get you all digits in a string, which will work for your example above.
  • mkl
    mkl about 7 years
    One should generally add some words of explanation to one's answer.