List<string> TrimStart elements

  • Thread starter Thread starter sjoshi
  • Start date Start date
S

sjoshi

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

List<string> lst = 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
 
sjoshi said:
Is List.ConvertAll the only way to apply TrimStart to elements of a
List<string> or is there a better way ?
Currently I'm doing this...

List<string> lst = 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));
 
Is List.ConvertAll the only way to apply TrimStart to elements of a
List<string> or is there a better way ?
Currently I'm doing this...

List<string> lst = 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top