1

I have the following method

/// <summary>
    /// Replaces SemiColons with commas because SMTP client does not accept semi colons
    /// </summary>
    /// <param name="emailAddresses"></param>
    public static List<string> ReplaceSemiColon(List<string> emailAddresses) // Note only one string in the list...
    {         
        foreach (string email in emailAddresses)
        {
            email.Replace(";", ",");
        }

        //emailAddresses.Select(x => x.Replace(";", ","));  // Not working either


        return emailAddresses;
    }

However the email string is not replacing the ";" with the ",". What am I missing?

Moojjoo
  • 729
  • 1
  • 12
  • 33

5 Answers5

4

I think you should try setting it back to itself email = email.Replace(";", ",");

Ryan Schlueter
  • 2,223
  • 2
  • 13
  • 19
4

String.Replace method returns new string. It doesn't change existing one.

Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.

As Habib mentioned, using foreach with the current list gets a foreach iteration variable error. It is a read-only iteration. Create a new list and then add replaced values to it instead.

Also you can use for loop for modifying existing list which keyboardP explained on his answer.

List<string> newemailAddresses = new List<string>();
foreach (string email in emailAddresses)
{         
     newemailAddresses.Add(email.Replace(";", ","));
}
return newemailAddresses;

Be aware since strings are immutable types, you can't change them. Even if you think you change them, you actually create new strings object.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    Soner, you can't modify iteration variable in foreach `Cannot assign to 'email' because it is a 'foreach iteration variable'` – Habib Sep 27 '13 at 14:26
  • @Habib Arrrhghhhh! Fool me `:)` Updated. I think it is ok now. – Soner Gönül Sep 27 '13 at 14:32
  • it looks good, but you can use a simple `for` loop and modify an existing list instead of creating a new one – Habib Sep 27 '13 at 14:35
  • @Habib You are right. I put a reference to [keyboardP's answer](http://stackoverflow.com/a/19053105/447156) which is a nice example how it can be done with `for` loop. – Soner Gönül Sep 27 '13 at 14:38
4

As others have already mentioned that strings are immutable (string.Replace would return a new string, it will not modify the existing one) and you can't modify the list inside a foreach loop. You can either use a for loop to modify an existing list or use LINQ to create a new list and assign it back to existing one. Like:

emailAddresses = emailAddresses.Select(r => r.Replace(";", ",")).ToList();

Remember to include using System.Linq;

Habib
  • 219,104
  • 29
  • 407
  • 436
3

Strings are immutable so another string is returned. Try

for(int i = 0; i < emailAddress.Count; i++)
{
   emailAddress[i] = emailAddress[i].Replace(";", ",");
}

A foreach loop would not compile here because you're trying to change the iteration variable. You'd run into this issue.

Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
0

You should use somthing like: var tmpList = new List(); add each modified email address to the tmplist and when you are done, return the TmpList. In .NET strings are inmutable, that's why your code doesnt work.

Ale Miralles
  • 604
  • 8
  • 17