String contains another two strings

前端 未结 17 1546
故里飘歌
故里飘歌 2020-12-15 07:12

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          


        
相关标签:
17条回答
  • 2020-12-15 07:39
    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();
    
    0 讨论(0)
  • 2020-12-15 07:40

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

    0 讨论(0)
  • 2020-12-15 07:42

    This is because d does not contain b + a (i.e. "someonedamage"), and therefore the application just terminates (since your Console.ReadLine(); is within the if block).

    0 讨论(0)
  • 2020-12-15 07:43

    I just checked for a space in contains to check if the string has 2 or more words.

    string d = "You hit someone for 50 damage";
    string a = "damage";
    string b = "someone";
    string c = "you";
    
    bool a = ?(d.contains(" ")):true:false;
    
    if(a)
    {
     Console.WriteLine(" " + d);
    }
    
    Console.Read();
    
    0 讨论(0)
  • 2020-12-15 07:43

    So what is that you are really after? If you want to make sure that something has hit for damage (in this case), why are you not using string.Format

    string a = string.Format("You hit someone for {d} damage", damage);
    

    In this way, you have the ability to have the damage qualifier that you are looking for, and are able to calculate that for other parts.

    0 讨论(0)
提交回复
热议问题