List<string> TrimStart elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sjoshi

    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(ne w string[] {"A test", " that you", " need to see!" });
    lst = lst.ConvertAll< string>(delegat e(string line) { return
    line.TrimStart( null); });

    thanks
    Sunit

  • Jon Skeet [C# MVP]

    #2
    Re: List&lt;string& gt; TrimStart elements

    sjoshi <sjoshi@ingr.co mwrote:
    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(ne w string[] {"A test", " that you", " need to see!" });
    lst = lst.ConvertAll< string>(delegat e(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 - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Peter Duniho

      #3
      Re: List&lt;string& gt; TrimStart elements

      On 2007-11-02 13:18:22 -0700, sjoshi <sjoshi@ingr.co msaid:
      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(ne w string[] {"A test", " that you", " need to see!" });
      lst = lst.ConvertAll< string>(delegat e(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

      Comment

      Working...