A tiny .NET library that removes indentation from multi-line strings.
Test coverage is 100%.
Unindent is available as a NuGet package.
This package provides an Unindent
extension method for .NET strings.
var result = input.Unindent();
The Unindent
method returns a string with the content of the input string,
but unindented: if all1 input lines begin with the
same amount of space (the indentation), that space is removed from the
returned string.
Space here means any mixture of the space (U+0020
) and tab (U+0009
)
characters. Tabs work like in most text editors:2 a
tab advances to the next tab stop. By default, tab stops are 8 spaces apart.
The optional tabStop
parameter overrides the default.
Lines end via any mixture of the carriage return (U+000D
) and line feed
(U+000A
) characters. Thus Unindent supports CR+LF
(Windows), LF
(Linux/Unix), and CR
(classic Mac OS) line endings.
Click here for details about some edge cases.
-
1 Unindent ignores blank lines (those containing only space) when discovering indentation in the input string, but the method still removes indentation from blank lines that have it. See this test for an example.
-
2 If a tab character jumps past the computed indentation width, that tab is replaced by space characters in order to preserve column alignments present in the input string. See this test for an example.
-
If the input string ends with trailing space, Unindent removes that space. See this test for an example.
Given this example code:
namespace Foo
{
public class Bar
{
const string Query = @"
-- Pretend there is a table called NaturalNumbers
SELECT TOP 100
Output = CASE
WHEN N % 15 = 0 THEN N'Fizz Buzz'
WHEN N % 5 = 0 THEN N'Buzz'
WHEN N % 3 = 0 THEN N'Fizz'
ELSE CONVERT(nvarchar(10), N)
END
FROM NaturalNumbers; -- 1, 2, 3, ...
";
}
}
The string constant Query
is indented by 12 spaces. To unindent it:
var query = Query.Unindent();
...which yields:
-- Pretend there is a table called NaturalNumbers
SELECT TOP 100
Output = CASE
WHEN N % 15 = 0 THEN N'Fizz Buzz'
WHEN N % 5 = 0 THEN N'Buzz'
WHEN N % 3 = 0 THEN N'Fizz'
ELSE CONVERT(nvarchar(10), N)
END
FROM NaturalNumbers; -- 1, 2, 3, ...