Remove empty lines at the beginning and at the end of a List<string>?
.net, c#, string
Solution
First off, there is a built in method that checks this: `String.IsNullOrWhiteSpace();`
The answer ColinE provides does not meet the requirement since it removes all lines that are empty, not just at the beginning or end.
I think you need to build your own solution:
int start = 0, end = sourceList.Count - 1;
while (start < end && String.IsNullOrWhiteSpace(sourceList[start])) start++;
while (end >= start && String.IsNullOrWhiteSpace(sourceList[end])) end--;
return sourceList.Skip(start).Take(end - start + 1);
Problem
Given a `List<string>` I need to remove all the empty lines at the beginning and at the end of the list. NOTE: I consider an empty line a line that has no content, but may contain whitespaces and tabs. This is the method to check if a line is empty: ``` private bool HasContent(string line) { if (string.IsNullOrEmpty(line)) return false; foreach (char c in line) { if (c != ' ' && c != '\t') return true; } return false; } ``` What efficient and readable code would you suggest to do that? Confirmed example ``` [" ", " ", " ", " ", "A", "B", " ", "C", " ", "D", " ", " ", " "] ``` Such a list should be trimmed, by removing all empty lines at the beginning and the end to the following result: ``` ["A", "B", " ", "C", " ", "D"] ```