Trim all rows in an array

I have a line that comes as:

string email = "a@a.com, b@b.com, c@c.com"; 

I want to split it into an array of strings

If I do this:

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

I get spaces before each email address (after the first):

 emails[0] = "a@a.com" emails[1] = " b@b.com" emails[2] = " c@c.com" 

What is the best way to get this (the best way to parse or the way to trim all rows in an array)?

 emails[0] = "a@a.com" emails[1] = "b@b.com" emails[2] = "c@c.com" 
+60
arrays c # trim
Aug 31 '09 at 3:50
source share
10 answers

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

 string email = "a@a.com, b@b.com, c@c.com"; string[] emails = email.Replace(" ", "").Split(','); 
+36
Aug 31 '09 at 4:00
source share
 emails.Split(',').Select(email => email.Trim()).ToArray() 
+212
Aug 31 '09 at 4:19
source share

One of the following will work. I would recommend the first, as it expresses the connection string more accurately.

 string[] emails = email.Split(new string[] { ", " }, StringSplitOptions.None); string[] emails = email.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); 
+20
Aug 31 '09 at 4:01
source share

You can use Trim ():

 string email = "a@a.com, b@b.com, c@c.com"; string[] emails = email.Split(','); emails = (from e in emails select e.Trim()).ToArray(); 
+10
Aug 31 '09 at 3:53
source share

Use Regex.Split to avoid cropping

 var emails = Regex.Split(email, @",\s*"); 
+8
Aug 31 '09 at 4:01
source share

You can use a single line solution like this:

 string[] emails = text.Split(',', StringSplitOptions.RemoveEmptyEntries); Array.ForEach<string>(emails, x => emails[Array.IndexOf<string>(emails, x)] = x.Trim()); 
+5
Feb 10 2018-11-11T00:
source share

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

 \s*,\s* 

i.e.

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

He will use the surrounding space directly.

Regular expressions usually refer to performance, but the example you provided indicates that this is what you plan to do once in your code for a short array.

+1
Aug 31 '09 at 3:57
source share

The answer from Brian Watts is elegant and simple. It implicitly refers to an array of strings created by Split ().

Also pay attention to its extensibility if you are reading a file and want to massage the data when building the array.

 string sFileA = @"C:\Documents and Settings\FileA.txt"; string sFileB = @"C:\Documents and Settings\FileB.txt"; // Trim extraneous spaces from the first file data string[] fileAData = (from line in File.ReadAllLines( sFileA ) select line.Trim()).ToArray(); // Strip a second unneeded column from the second file 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 want.

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

Although my answer should have been posted as a comment, I have no comments for comments yet. But I found this discussion invaluable in determining how to massage data using ReadAllLines ().

+1
Mar 17 '16 at 20:33
source share

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

0
Aug 31 '09 at 3:54
source share

If you just need to manipulate records without returning an array:

 string[] emails = text.Split(','); Array.ForEach(emails, e => e.Trim()); 
0
Jul 18 '19 at 16:05
source share



All Articles