I am trying to indent block of strings in a StringBuilder, but what I am getting is just the indentation of 1st line. I understand why it is doing so, but don't know if there is any easy way to achieve what I am looking for.
void Main()
{
var subLines = new StringBuilder();
subLines.AppendLine("Sub Line1");
subLines.AppendLine("Sub Line2");
subLines.AppendLine("Sub Line3");
var mainLines = new StringBuilder();
mainLines.AppendLine("Main Line 1");
mainLines.AppendLine("Main Line 2");
mainLines.Append("\t").Append(subLines.ToString());
mainLines.AppendLine("Main Line 3");
mainLines.ToString().Dump();
}
Actual Output
Main Line 1
Main Line 2
Sub Line1
Sub Line2
Sub Line3
Main Line 3
Expected Output
Main Line 1
Main Line 2
Sub Line1
Sub Line2
Sub Line3
Main Line 3
I have also tried using IndentedTextWriter
, but gives the same results.
public static class StringBuilderExtensions
{
public static StringBuilder AppendIndented(this StringBuilder sb, string text)
{
var textWriter = new StringWriter(sb);
var indentWriter = new IndentedTextWriter(textWriter, "\t\t");
indentWriter.WriteLine("");
indentWriter.Indent = 1;
indentWriter.WriteLine(text);
return sb;
}
}
Any thoughts on how can I achieve block indentation?