hi, i wants to ask some string problems, as below:

string []first
1 2 3 4 5
1 4 5
1 3 4 5

string second="2 3";

double sum=0;
foreach(string a in first)
{
if(     ) // what should i write here?
{
sum++;
}
}

In program above, if string array first contains string second, sum will +1. How should i write the code?

Do you mean an exact match( "1 2" == "1 2") or if the array element contains string second as a substring ("1 2" is in "3 1 2 4")?

"the array element contains string second as a substring ("1 2" is in "3 1 2 4")", this what i means..

        string[] first=new string[3];
        int sum = 0;
        first[0] = "1 2 3 4 5";
        first[1] = "1 4 5";
        first[2] = "1 3 4 5";
        string second= "2 3";
        foreach (string a in first)
        {
            if (a.Contains(second))
                sum++;
        }
        Console.WriteLine(sum);
        Console.ReadLine();

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Check this example:

string[] array1 = { "12345", "145", "1345" };
            string second= "23";
            int sum = 0;
            foreach (string item in array1)
            {
                if (item.Contains(second))
                    sum++;
            }
            MessageBox.Show("There is/are " + sum + " strings of \"" + second + "\" string in the array.");
This question has already been answered. Start a new discussion instead.