How to increment the array element in a FOREACH loop

I have the following code:

foreach (string word in words)

{

//do stuff to each word, as go through the loop

}

My question is: i want to check inside the loop if the word = "car", and if it does, then i want to concatenate that word with the next word in the words array.

My code assumes all mentions of the word car is part of the word carpool, and some users enter it as two words.



Answer this question

How to increment the array element in a FOREACH loop

  • Magic PC

    Hi,



    int i=0;
    foreach (string word in words)
    {
    if(word == "car")
    {
    words[i+1] += word;
    }
    i++;
    }

    Or use standard "for" loop,

    HTH,



  • A. Argueta

    Hi,

    you cannot increment past an item in a foreach loop, you will need to use a standard for loop in this case where you can increment the index manually.

    Mark.



  • How to increment the array element in a FOREACH loop