-4

Lets say I have the following string:

  string str = "aaa123aaa";

And I convert the given string to char array:

 char[] array1 = str.ToArray();

Then I want to use foreach to loop through the elements to change all numbers to whitespace

foreach (char i in array1)
        {
            if (char.IsNumber(i)) { //   ? }
        }

How exactly do I change the character matching the criteria to whitespace? I've tried the following to no avail:

i = ' '; char(i) = ' '; array1(i) = ' ';

None of them work.

How exactly do I interact with the elements in foreach loop? It's simple if I use array, in that case I would simply use:

array1[i] = ' ';

But I've no idea how to do the same in foreach loop.

Thank you in advance!

Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
  • You don't want `foreach` which is designed for read-only applications, you want to use a normal `for` loop. – Guvante Feb 04 '15 at 17:15
  • use a for loop if you want to modify your collection within – Pankaj Feb 04 '15 at 17:16
  • Note that while it is very similar to [modify collection in foreach](http://stackoverflow.com/questions/759966/what-is-the-best-way-to-modify-a-list-in-a-foreach-loop) it is likely different as OP needs a way to get from item during iteration back to element of collection. Clearly modifying collection in most cases would hit "collection modified" exception as covered in link above.. – Alexei Levenkov Feb 04 '15 at 17:36

3 Answers3

8

you can't change the elements of a collection if a foreach loop - but you can with a for loop:

for(int i=0; i < array1.Length; i++)
{
    if (char.IsNumber(array1[i])) { array1[i] = ' ' }
}

Note that this does not change the original string - if you want to do that, there are more effective ways that converting to a char array.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • In a sense, yes - `foreach` gives you each _item_ from a collection (and any attempts to modify the collection within the loop usually generate an exception) – D Stanley Feb 04 '15 at 17:19
  • @dontseeverysharp - That's a pretty universal concept: You can't change the item you're enumerating through while you're enumerating it. Sometimes it won't work, sometimes it will throw an exception. That's why the integer increment loop works - you never use the enumerator of the array you're modifying. – Wesley Long Feb 04 '15 at 17:21
  • "can't change the elements of collection" is not completely true - you can easily change array elements (as it does not have protection against changing element unlike `List`), but `foreach` does not really give you index of item to change - so `for` solves both - get index and safe update irrespective of collection type. – Alexei Levenkov Feb 04 '15 at 17:41
5

You can do it using a for loop instead or you can also use LINQ

char[] array1 = str.Select(x => char.IsNumber(x) ? ' ' : x).ToArray();
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

If you replacement is simple, why not use the .NET Regex class and replace the numbers based on the pattern?

string test = "aaa123aaa";
test = Regex.Replace(test, "[0-9]", " ");