leora
leora

Reputation: 196469

trim all strings in an array

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]"

Upvotes: 85

Views: 93176

Answers (12)

Dan
Dan

Reputation: 7724

In .NET 5, they added StringSplitOptions.TrimEntries.

You can use it like

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

Which will give you

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

Upvotes: 0

formicini
formicini

Reputation: 348

.NET 5 has arrived and with it, a modern solution to this problem:

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

Upvotes: 7

Teodor Tite
Teodor Tite

Reputation: 1955

If you just need to manipulate the entries, without returning the array:

string[] emails = text.Split(',');
Array.ForEach(emails, e => e.Trim());

Upvotes: 4

RonSanderson
RonSanderson

Reputation: 51

The answer from Bryan Watts is elegant and simple. He implicitly refers to the array of strings created by the Split().

Also note its extensibility if you are reading a file, and want to massage the data while building an array.

string sFileA = @"C:\Documents and Settings\FileA.txt";
string sFileB = @"C:\Documents and Settings\FileB.txt";

// Trim extraneous spaces from the first file's data
string[] fileAData = (from line in File.ReadAllLines( sFileA )
                      select line.Trim()).ToArray();

// Strip a second unneeded column from the second file's data
string[] fileBData = (from line in File.ReadAllLines( sFileB )
                      select line.Substring( 0, 21 ).Trim()).ToArray();

Of course, you can use the Linq => notation if you prefer.

string[] fileBData = File.ReadAllLines( sFileB ).Select( line =>
                             line.Substring( 0, 21 ).Trim()).ToArray();

Although my answer should have been posted as a comment, I don't have enough reputation points to comment yet. But I found this discussion invaluable in figuring out how to massage data while using ReadAllLines().

Upvotes: 1

nick2083
nick2083

Reputation: 1973

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(',');

Upvotes: 42

mr.martan
mr.martan

Reputation: 213

You can use a one line solution like this:

string[] emails = text.Split(',', StringSplitOptions.RemoveEmptyEntries);
Array.ForEach<string>(emails, x => emails[Array.IndexOf<string>(emails, x)] = x.Trim());

Upvotes: 5

Bryan Watts
Bryan Watts

Reputation: 45445

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

Upvotes: 265

Jan Zich
Jan Zich

Reputation: 15323

Alternatively, you can split using a regular expression of the form:

\s*,\s*

i.e.

string[] emails = Regex.Split(email, @"\s*,\s*");

It will consume the surrounding spaces directly.

Regular expressions are usually a performance hit, but the example you gave indicates that this is something you plan to do once in your code for a short array.

Upvotes: 2

skalb
skalb

Reputation: 5567

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();

Upvotes: 11

Sam Harwell
Sam Harwell

Reputation: 99869

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);

Upvotes: 23

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

Use Regex.Split to avoid trimming

var emails = Regex.Split(email, @",\s*");

Upvotes: 8

jscharf
jscharf

Reputation: 5899

Use String.Trim in a foreach loop, or if you are using .NET 3.5+ a LINQ statement.

Upvotes: 0

Related Questions