C# 字符串包含另外两个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16037445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String contains another two strings
提问by Winkz
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:
如果字符串包含 2 个或更多单词,是否可以使用包含函数查找?这就是我想要做的:
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?
还有一个问题:如果我想增加造成的伤害,那么获得该数字并将其放入 的最简单方法是TryParse什么?
采纳答案by Brian Dishaw
You would be better off just calling Containstwice or making your own extension method to handle this.
您最好只调用Contains两次或创建自己的扩展方法来处理此问题。
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.
至于您的另一个问题,您可以构建一个正则表达式来解析字符串以找到 50 或者如果字符串始终相同,只需根据空格将其拆分并获得第 5 部分。
回答by Richard Ev
This is because ddoes notcontain b + a(i.e. "someonedamage"), and therefore the application just terminates (since your Console.ReadLine();is within the ifblock).
这是因为d它不含有b + a(即"someonedamage"),因此应用程序只是终止(因为你Console.ReadLine();是内if块)。
回答by Tralli
That is because the if statements returns false since d doesn't contain b + a i.e "someonedamage"
那是因为 if 语句返回 false,因为 d 不包含 b + a 即“someonedamage”
回答by Arshad
Because b + a ="someonedamage", try this to achieve :
因为b + a ="someonedamage",试试这个来实现:
if (d.Contains(b) && d.Contains(a))
{
Console.WriteLine(" " + d);
Console.ReadLine();
}
回答by Soner G?nül
Your b + ais equal "someonedamage", since your ddoesn't contain that string, your ifstatement returns falseand doesn't run following parts.
您b + a是 equal "someonedamage",因为您d不包含该字符串,您的if语句返回false并且不运行以下部分。
Console.WriteLine(" " + d);
Console.ReadLine();
You can control this more efficient as;
您可以更有效地控制它;
bool b = d.Contains(a) && d.Contains(b);
Here is a DEMO.
这是一个DEMO.
回答by Rik
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();
回答by Tomtom
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.
使用代码d.Contains(b + a)检查“你击中某人造成 50 点伤害”是否包含“someonedamage”。而这(我猜)你不想要。
The + concats the two string of b and a.
+ 连接了 b 和 a 的两个字符串。
You have to check it by
你必须检查它
if(d.Contains(b) && d.Contains(a))
回答by Tim Schmelter
So you want to know if one string contains two other strings?
所以你想知道一个字符串是否包含另外两个字符串?
You could use this extension which also allows to specify the comparison:
您可以使用此扩展名,它还允许指定比较:
public static bool ContainsAll(this string text, StringComparison comparison = StringComparison.CurrentCulture, params string[]parts)
{
return parts.All(p => text.IndexOf(p, comparison) > -1);
}
Use it in this way (you can also omit the StringComparison):
以这种方式使用它(您也可以省略StringComparison):
bool containsAll = d.ContainsAll(StringComparison.OrdinalIgnoreCase, a, b);
回答by Conrado Costa
If you have a list of words you can do a method like this:
如果你有一个单词列表,你可以使用这样的方法:
public bool ContainWords(List<string> wordList, string text)
{
foreach(string currentWord in wordList)
if(!text.Contains(currentWord))
return false;
return true;
}
回答by Serge
You could write an extension method with linq.
您可以使用 linq 编写扩展方法。
public static bool MyContains(this string str, params string[] p) {
return !p.Cast<string>().Where(s => !str.Contains(s)).Any();
}
EDIT (thx to sirid):
编辑(感谢sirid):
public static bool MyContains(this string str, params string[] p) {
return !p.Any(s => !str.Contains(s));
}

