Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String contains another two strings

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?

like image 368
Winkz Avatar asked Apr 16 '13 12:04

Winkz


2 Answers

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.

like image 71
Brian Dishaw Avatar answered Oct 03 '22 07:10

Brian Dishaw


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");
}
like image 40
Dustin Kingen Avatar answered Oct 03 '22 08:10

Dustin Kingen