I have a string[]
which contains code. Each line contains some leading spaces. I need to 'unindent' the code as much as possible without changing the existing formatting.
For instance the contents of my string[]
might be
public class MyClass { private bool MyMethod(string s) { return s == ""; } }
I'd like to find a reasonably elegant and efficient method (LINQ?) to transform it to
public class MyClass { private bool MyMethod(string s) { return s == ""; } }
To be clear I'm looking for
IEnumerable<string> UnindentAsMuchAsPossible(string[] content)
{
return ???;
}
Building on Tim Schmelter's answer:
static IEnumerable<string> UnindentAsMuchAsPossible(IEnumerable<string> lines, int tabWidth = 4)
{
if (!lines.Any())
{
return Enumerable.Empty<string>();
}
var minDistance = lines
.Where(line => line.Length > 0)
.Min(line => line
.TakeWhile(Char.IsWhiteSpace)
.Sum(c => c == '\t' ? tabWidth : 1));
var spaces = new string(' ', tabWidth);
return input
.Select(line => line.Replace("\t", spaces))
.Select(line => line.Substring(Math.Min(line.Length, minDistance)));
}
This handles:
Just count the number of leading spaces on the first line, and then "remove" that many characters from the start of each line:
IEnumerable<string> UnindentAsMuchAsPossible(string[] content)
{
int spacesOnFirstLine = content[0].TakeWhile(c => c == ' ').Count();
return content.Select(line => line.Substring(spacesOnFirstLine));
}
This should work:
static IEnumerable<string> UnindentAsMuchAsPossible(IEnumerable<string> input)
{
int minDistance = input.Min(l => l.TakeWhile(Char.IsWhiteSpace).Count());
return input.Select(l => l.Substring(minDistance));
}
It moves the code to the left, all lines with the same number of spaces.
For example:
string testString = @"
public class MyClass
{
private bool MyMethod(string s)
{
return s == "";
}
}";
string[] lines = testString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
string[] unindentedArray = UnindentAsMuchAsPossible(lines).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With