trim all strings in an array

Solution 1:

emails.Split(',').Select(email => email.Trim()).ToArray()

Solution 2:

You could also replace all occurrences of spaces, and so avoid the foreach loop:

string email = "[email protected], [email protected], [email protected]";    
string[] emails = email.Replace(" ", "").Split(',');

Solution 3:

Either one of the following would work. I'd recommend the first since it more accurately expresses the joining string.

string[] emails = email.Split(new string[] { ", " }, StringSplitOptions.None);
string[] emails = email.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

Solution 4:

You can use Trim():

string email = "[email protected], [email protected], [email protected]";
string[] emails = email.Split(',');
emails = (from e in emails
          select e.Trim()).ToArray();