How can I trim a List<string> so preceding and succeeding blank lines are removed?
c#, generics, string, trim
Solution
What 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);
}
}
Note that the signature has changed from your example (return type is void).
Problem
What is the easiest way to do this? The results should be: ``` 1: one 2: two 3: 4: 5: five ``` 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) { //??? } } } ```