StringBuilder 和缩进

StringBuilder and Indentation

我试图缩进 StringBuilder 中的字符串块,但我得到的只是第一行的缩进。我明白为什么会这样,但不知道是否有任何简单的方法可以实现我想要的。

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();
}

实际输出

Main Line 1
Main Line 2
    Sub Line1
Sub Line2
Sub Line3
Main Line 3

预期输出

Main Line 1
Main Line 2
   Sub Line1
   Sub Line2
   Sub Line3
Main Line 3

我也尝试过使用 IndentedTextWriter,但给出了相同的结果。

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;
    }
}

关于如何实现块缩进有什么想法吗?

这可能有帮助

      static void Main()
    {
        var subLines = new StringBuilder();
        subLines.AppendLine("Sub Line1");
        subLines.AppendLine("Sub Line2");
        subLines.AppendLine("Sub Line3");

        var mainLines = new StringBuilder();
                   
        ApplyIndentation(mainLines,subLines);
      
        mainLines.ToString();
        Console.WriteLine(mainLines);
      

    }
    private static void ApplyIndentation(StringBuilder mainLines, StringBuilder subLines)
    {
        
        string[] delim = { Environment.NewLine, "\n" };
        string[] lines = subLines.ToString().Split(delim, StringSplitOptions.None);
        mainLines.AppendLine("Main Line 1");
        mainLines.AppendLine("Main Line 2");
        foreach (string line in lines)
        {
            mainLines.Append(Indent(3)).AppendLine(line);
            
        }
        mainLines.AppendLine("Main Line 3");

    }
    public static string Indent(int count)
    {
        return "".PadLeft(count);
    }

从 canton7 的评论中得到提示,这是我为实现结果而创建的扩展方法。

  public static class StringBuilderExtensions
    {
        public static StringBuilder AppendIndented(this StringBuilder sb, string textBlock)
        {
            foreach (var line in textBlock.TrimEnd().Split('\n'))
                if (!string.IsNullOrWhiteSpace(line))
                    sb.AppendLine($"\t{line}");
            return sb;
        }
    }

用法

    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.AppendIntended(sublines);
    mainLines.AppendLine("Main Line 3");
    

您可以创建自己的 StringBuilder 实现,以匹配 Debug.Indent() 和 Debug.UnIndent()

的调试方法
public class StringBuilderIndented
{
    private StringBuilder sb;

    public string _Indent = "";

    public StringBuilderIndented()
    {
        sb = new StringBuilder();
    }

    public void Indent()
    {
        _Indent += new string(' ', 5);
    }

    public void WriteLine(string str)
    {
        sb.AppendLine(_Indent+str);
    }
    public void Unindent()
    {
        if (_Indent.Length >= 5)
            _Indent = new string(' ', _Indent.Length - 5);
        else
            _Indent = "";
    }

    public override string ToString()
    {
        return sb.ToString();
    }

}

用法:

然后在使用 Debug 编写的代码中,您可以将其替换为您的 stringbuilder 调用

    private static void WriteLog()
    {
        var Debug = new StringBuilderIndented();
        Debug.WriteLine("Main Line 1");
        Debug.WriteLine("Main Line 2");
        Debug.Indent();
        Debug.WriteLine("Sub Line 1");
        Debug.WriteLine("Sub Line 2");
        Debug.Indent();
        Debug.WriteLine("Sub Sub Line 1");
        Debug.WriteLine("Sub Sub Line 2");
        Debug.Unindent();
        Debug.WriteLine("Sub Line 3");
        Debug.Unindent();
        Debug.WriteLine("Main Line 3");
        Trace.WriteLine(Debug.ToString());
    }
    

输出:

Main Line 1
Main Line 2
     Sub Line 1
     Sub Line 2
          Sub Sub Line 1
          Sub Sub Line 2
     Sub Line 3
Main Line 3