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?

Recommended Answers

All 6 Replies

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();

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.");

problem solved, thanks every1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.