问题描述
我需要检查2个字符串,如果temp包含temp2中的某些或所有项目,则应确定为正常;如果temp2不包含temp2中的所有项目,则应为否.
例如:
I need to check 2 stings, if temp contains some or all the items in temp2 should get OK, if temp2 does not contain all items in temp I should get NO
example:
temp2= "C# Java Delphi Linux PHP"
temp = "C# PHP"
if (temp2.Contains(temp))
{
MessageBox.Show("YES I Want " + temp + " User has : " + temp2);
}
else
{
MessageBox.Show("NO I Want " + temp + " User has : " + temp2);
}
在这种情况下,我会收到无消息"
in this case I get the NO Message
temp2 = "C# Java Delphi Linux PHP"
temp = "Java Delphi"
在这种情况下,我会收到是"消息.
in this case I get the YES Message.
can you please help me out.
推荐答案
string searchIn = "C# Java Delphi Linux PHP";
string searchFor = "C# PHP";
string[] findThese = searchFor.Split(' ');
bool hasAll = true;
foreach (string s in findThese)
{
if (!searchIn.Contains(s))
{
hasAll = false;
break;
}
}
if (hasAll)
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
"10ks,这些问题确实解决了.您不能为我注释一下代码,因为我不完全了解它的功能.-41分钟前bmw318mt"
你在开玩笑吧? :laugh:
你不是在开玩笑吧?哦,亲爱的,那将是那些日子之一...
首先,不要在这里使用文本说话:这会惹恼某些人,并且使非英语母语的人感到困惑-他们无法在词典中查找它,因此他们无法弄清您在说什么.这里有很多非本地的英国人...
"10ks, these really work out. can you please comment the code for me as i can''t understand exactly what it does. - bmw318mt 41 mins ago"
You are kidding, right? :laugh:
You aren''t kidding, are you? Oh, dear, it''s going to be one of those days...
Firstly, don''t use textspeak here: it annoys some people, and it confuses the non-native English speakers - they can''t look it up in a dictionary, so they can''t work out what you are saying. There are a lot of non-native English people here...
string searchIn = "C# Java Delphi Linux PHP";
声明一个名为"searchIn"的字符串变量-您将其称为"temp",但是如果您始终使用描述名称,则可以更轻松地弄清正在发生的情况(VS Intellisense自动完成,因此也省事了). br/>它为它分配一个字符串,其中包含要搜索的文本.通常,我希望这是方法的参数.
Declares a string variable, called "searchIn" - you called this "temp" but it makes it a lot easier to work out what is happening if you always use description names (VS Intellisense auto completes so it is less effort as well).
It assigns a string to it which contains the text you want to search. Normally,I would expect this to be a parameter to a method.
string searchFor = "C# PHP";
声明第二个字符串变量,称为"searchFor",并为其分配要搜索的术语(因此命名为...).
同样,通常是方法参数.
Declares a second string variable, called "searchFor", and assigns it the terms you want to search for (hence the name...).
Again, normally a method parameter.
string[] findThese = searchFor.Split('' '');
声明一个称为"findThese"的字符串变量数组.
"searchFor"字符串分为多个单独的字符串,每个字符串都由一个空格分隔.因此,在"searchFor"字符串为"C#PHP"的情况下,拆分将创建两个新字符串,即"C#"和"PHP".它们一起收集到一个字符串数组中,并分配给"findThese"变量.
Declares an array of string variables called "findThese".
The "searchFor" string is split into separate strings, each delimited by a space. So, with the "searchFor" string being "C# PHP", the Split would create two new strings, "C#" and "PHP". These are collected together into an array of strings, and assigned to the "findThese" variable.
bool hasAll = true;
声明一个名为"hasAll"的变量,该变量只能具有两个值之一:"true"和"false".默认为"true".
Declares a variable called "hasAll" which can have one of two values only: "true" and "false". Defaults it to being "true".
foreach (string s in findThese)
建立一个循环,从字符串数组"findThese"中提取每个字符串,然后依次将其分配给字符串变量"s".
在上面的示例中,循环主体中的代码将执行两次,第一次使用"s"等于"C#",第二次使用"s"等于"PHP"
Sets up a loop, extracting each string from the string array "findThese" and assigning it in turn to the string variable "s".
In the example above, the code in the body of the loop will be executed twice, the first time with "s" equal to "C#" and the second with "s" equal to "PHP"
{
指示循环主体的开始
Indicates the start of the loop body
if (!searchIn.Contains(s))
搜索字符串"searchIn"以查看当前值"s"是否在其中.如果不是(如!"字符所示),则执行其下面的代码块.
如果字符串在其中,请不要执行下面的代码
Searches the string "searchIn" to see if the current value of "s" is within it. If it is not (as indicated by the ''!'' character) then execute the code block below it.
If the string is in there, do not execute the code below
{
表示如果找不到该字符串,则要执行的代码的开始.
Indicates the start of code to be executed if the string is not found.
hasAll = false;
因为输入中至少没有一个为字符串填充的小节,我们需要标记输入不为
包含我们想要的所有字符串.将"hasAll"的值更改为"false" =与
开始的相反
Because at least one of the serached for strings is not in the input, we need to mark that the input does not
contain all the strings we want. Changes the value of "hasAll" to "false" = the opposite of what it started off with
break;
因为我们知道输入字符串并不包含我们要查找的所有字符串,所以我们无需再查找-立即退出循环.
Because we know the input string does not contain all the strings we are looking for, we don''t need to look any more - exit from the loop immediately.
}
指示条件执行的代码的结尾.
Indicates the end of the conditionally executed code.
}
指示循环结束.
Indicates the end of the loop.
if (hasAll)
如果我们发现所有需要的字符串都在输入字符串中,请执行以下代码.
If we found all strings that were wanted were in the input string, execute the code below.
{
表示如果搜索到的字符串全部在其中,则要执行的代码的开始.
Indicates the start of code to be executed if the searched for strings were all there.
Console.WriteLine("Yes");
将消息是"写入控制台.
Writes the message "Yes" to the console.
}
指示要有条件执行的代码结尾
Indicates the end of code to be conditionally executed
else
如果我们执行了上面的代码,请不要执行下面的代码.
如果我们没有执行上面的代码,请执行下面的代码.
If we executed the code above, don''t execute the code below.
If we didn''t execute the code above, then execute the code below.
{
表示如果搜索的字符串还不存在,则执行代码的开始.
Indicates the start of code to be executed if the searched for strings were not all there.
Console.WriteLine("No");
将消息否"写入控制台.
Writes the message "No" to the console.
}
指示要有条件执行的代码结尾
不要指望有人再为您做一次!
编写说明所花的时间比编写代码所花的时间长十倍...
Indicates the end of code to be conditionally executed
Do not expect anyone to do that for you again!
It took about ten times longer to write the explanation than it did to write the code...
这篇关于字符串包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!