Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trim all strings in an array

Tags:

arrays

c#

.net

trim

I have a string that comes in like:

string email = "[email protected], [email protected], [email protected]"; 

I want to split it into an array of strings

If I do this:

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

I get spaces in front of each email address (after the first one):

emails[0] = "[email protected]" emails[1] = " [email protected]" emails[2] = " [email protected]" 

What is the best way to get this (either a better way to parse or a way to trim all strings in an array)?

emails[0] = "[email protected]" emails[1] = "[email protected]" emails[2] = "[email protected]" 
like image 339
leora Avatar asked Aug 31 '09 03:08

leora


People also ask

How do you cut all values in an array?

To trim all strings in an array:Use the map() method to iterate over the array and call the trim() method on each array element. The map method will return a new array, containing only strings with the whitespace from both ends removed.


1 Answers

emails.Split(',').Select(email => email.Trim()).ToArray() 
like image 192
Bryan Watts Avatar answered Sep 19 '22 13:09

Bryan Watts