0

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?

4
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by leo88
Featured Replies
0

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")?

0

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

0
        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 by mike_2000_17: Fixed formatting

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.