How can I trim the List <string> so the previous and next empty lines are deleted?

What is the easiest way to do this?

The results should be:

1: one 2: two 3: 4: 5: five 

the code:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestLines8833 { class Program { static void Main(string[] args) { List<string> lines = new List<string>(); lines.Add(""); lines.Add("one"); lines.Add("two"); lines.Add(""); lines.Add(""); lines.Add("five"); lines.Add(""); lines.Add(""); lines.TrimList(); } } public static class Helpers { public static List<string> TrimList(this List<string> list) { //??? } } } 
+6
string generics c # trim
source share
8 answers

How about this:

  public static void TrimList(this List<string> list) { while (0 != list.Count && string.IsNullOrEmpty(list[0])) { list.RemoveAt(0); } while (0 != list.Count && string.IsNullOrEmpty(list[list.Count - 1])) { list.RemoveAt(list.Count - 1); } } 

Please note that the signature has changed from your example (return type is not valid).

+6
source share

Ok, now I understand the desired results:

 public static class Helpers { // Adjust this to use trimming, avoid nullity etc if you you want private static readonly Predicate<string> NonBlankLinePredicate = x => x.Length != 0; public static List<string> TrimList(this List<string> list) { int start = list.FindIndex(NonBlankLinePredicate); int end = list.FindLastIndex(NonBlankLinePredicate); // Either start and end are both -1, or neither is if (start == -1) { return new List<string>(); } return list.GetRange(start, end - start + 1); } } 

Note that this does not modify the existing list - it returns a new list with the desired content. It was not clear what behavior you wanted, given that you gave the method a return type, but your sample calls it without using the result. Personally, I prefer methods without side effects, although it may be worth changing the name :)

+9
source share

Try the following:

 public static List<string> TrimList(this List<string> list) { return list.SkipWhile(l => String.IsNullOrEmpty(l)).Reverse().SkipWhile(l => String.IsNullOrEmpty(l)).Reverse(); } 
+5
source share

An old question that I know, but here is an extension method that will clip objects from the beginning and end of a collection based on a logical delegate using Linq.

 public static class IEnumerableExtensions { public static IEnumerable<T> Trim<T>( this IEnumerable<T> collection, Func<T, bool> trimCondition ) { return collection.SkipWhile( trimCondition ).Reverse().SkipWhile( trimCondition ).Reverse(); } } 

An example for your case:

 lines.Trim(line => string.IsNullOrEmpty(line)); 
+1
source share

If you want to remove blank lines, you can do something like this ...

 lines = lines.Where(s => ! string.IsNullOrEmpty(s)).ToList(); 

Update: Sorry that you just saw your editing, that you want to keep internal spaces.

In this case, I would just have an extension method like the one you mentioned above, since I don't think there is an easier way to do this.

0
source share

There is nothing that could do something specific. You can check the elements from the beginning and the end and delete blank lines. To minimize the operations in the list (reusing RemoveAt to remove the first item is pretty inefficient), first count the number of items to remove, and then use the RemoveRange method to remove them all at once.

To match how you use the method in code, the extension must modify the list, not return a new list.

 public static void TrimList(this List<string> list) { int cnt = 0; while (cnt < list.Count && list[cnt].Length == 0) cnt++; if (cnt > 0) list.RemoveRange(0, cnt); cnt = 0; while (cnt < list.Count - 1 && list[list.Count - cnt - 1].Length == 0) cnt++; if (cnt > 0) list.RemoveRange(list.Count - cnt, cnt); } 
0
source share

Once upon a time, the good old foreach beats linq both in terms of readability and in terms of performance:

 public static List<string> TrimList(this List<string> list) { list.TrimListStart(); vat l = list.Reverse().ToList(); l.TrimListStart(); return l; } public void TrimListStart(this List<string> list) { foreach(var s in new List(list)) { if(string.string.IsNullOrWhiteSpace(s)) { list.Remove(s); } else { break; } } } 
0
source share
  int start = stringList.FindIndex((i => i.Trim() != "")); int end = stringList.FindLastIndex((i => i.Trim() != "")); List<string> range = new List<string>(); if(start != -1 && end != -1) range = stringList.GetRange(start, (end - start + 1)); 
0
source share

All Articles