简体   繁体   中英

How can i skip each 9 items when adding to a List?

public void ImagesLinks()
{
    int count = 0;
    foreach(string c in DatesAndTimes)
    {
        if (count == 9)
        {
            count = 0;
        }
        string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true";
        imagesUrls.Add(imageUrl);
        count++;
    }
}

The List DatesAndTimes is in this format: Each line is item in the List.

201612281150201612281150
201612281150201612281151
201612281150201612281152
201612281150201612281153
201612281150201612281154
201612281150201612281155
201612281150201612281156
201612281150201612281157
201612281150201612281158
201612281150201612281159
Europe
201612281150201612281150
201612281150201612281151
201612281150201612281152
201612281150201612281153
201612281150201612281154
201612281150201612281155
201612281150201612281156
201612281150201612281157
201612281150201612281158
201612281150201612281159
Turkey

Every 10 items there is a name. I want to skip the name when i build the links. So i count to 9 each time ( count 10 times from 0 to 9 ) then i reset the counter to 0 and i also want to skip the name for example Europe then count to 10 again and skip Turkey and so on.

You can also use (count % 9 == 0) , this way you don't have to reset the counter:

public void ImagesLinks()
{
    int count = 0;
    foreach(string c in DatesAndTimes)
    {
        if (count++ % 9 == 0)
            continue;

        string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true";
        imagesUrls.Add(imageUrl);
    }
}

Use continue in the if statement. It will skip the line after and resume the foreach loop execution.

public void ImagesLinks()
            {
                int count = 0;
                foreach(string c in DatesAndTimes)
                {
                    if (count == 9)
                    {
                        count = 0;
                        continue;
                    }
                    string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true";
                    imagesUrls.Add(imageUrl);
                    count++;
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM