Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
temp2= "C# Java Delphi Linux PHP"
temp = "C# PHP"


C#
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

C#
temp2 = "C# Java Delphi Linux PHP"
temp = "Java Delphi"


in this case I get the YES Message.

can you please help me out.
Posted
Updated 12-Dec-10 8:36am
v4
Comments
OriginalGriff 12-Dec-10 7:09am    
Answer updated

1 solution

There is no simple function that does that, you are going to have to check for them all separately:

C#
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, 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";

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";
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(' ');
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;
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)
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))
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;
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...
 
Share this answer
 
v2
Comments
datt265 12-Dec-10 6:02am    
10ks, these really work out. can you please comment the code for me as i can't understand exactly what it does.
#realJSOP 12-Dec-10 7:07am    
@bmw318mt - Seriously? This is very basic code. If you need an explanation and can't work it out on your own, maybe you should consider another line of work.
OriginalGriff 12-Dec-10 7:10am    
John, I think he is an absolute beginner - we all had to start somewhere!
Mind you, if I'd realized how long it was going to take to document I probably wouldn't have started... :laugh:
datt265 12-Dec-10 9:07am    
thank you for your patience, yes i am a beginner, I could not understand most of it, and especially this part got me confused.
string[] findThese = searchFor.Split(' ');

I SAY THANK YOU VERY MUCH AGAIN, I REALLY APPROPRIATE IT
fjdiewornncalwe 12-Dec-10 10:17am    
Super awesome supercalifragilistic answer Griff.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900