Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...