471,119 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

List<string> TrimStart elements

Is List.ConvertAll the only way to apply TrimStart to elements of a
List<stringor is there a better way ?
Currently I'm doing this...

List<stringlst = new List<string>();
lst.AddRange(new string[] {"A test", " that you", " need to see!" });
lst = lst.ConvertAll<string>(delegate(string line) { return
line.TrimStart(null); });

thanks
Sunit

Nov 2 '07 #1
2 5203
sjoshi <sj****@ingr.comwrote:
Is List.ConvertAll the only way to apply TrimStart to elements of a
List<stringor is there a better way ?
Currently I'm doing this...

List<stringlst = new List<string>();
lst.AddRange(new string[] {"A test", " that you", " need to see!" });
lst = lst.ConvertAll<string>(delegate(string line) { return
line.TrimStart(null); });
That's fine, yes. Nicer in C# 3, of course:

lst = lst.ConvertAll(line =line.TrimStart(null));

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 2 '07 #2
On 2007-11-02 13:18:22 -0700, sjoshi <sj****@ingr.comsaid:
Is List.ConvertAll the only way to apply TrimStart to elements of a
List<stringor is there a better way ?
Currently I'm doing this...

List<stringlst = new List<string>();
lst.AddRange(new string[] {"A test", " that you", " need to see!" });
lst = lst.ConvertAll<string>(delegate(string line) { return
line.TrimStart(null); });
Since the String class is immutable, I think the List.ConvertAll()
method is a reasonably nice solution. You'll need _some_ sort of code
that replaces each list element with a new item, and that's pretty much
what ConvertAll() is for.

Other than an explicit for() loop indexing each list element, I don't
see any obvious alternative that is similarly compact and easy-to-read.

Is there something specific about List.ConvertAll() that you don't like?

Pete

Nov 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by s | last post: by
3 posts views Thread by aquanutz | last post: by
4 posts views Thread by parez | last post: by
3 posts views Thread by banangroda | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.