26 Haz

C# Indent String With Spaces

This example shows how to indent strings using method for padding in C#. “String.PadLeft” static method is used to repeat spaces.

Usage:

            Console.WriteLine(Indent(0) + "Versions");
            Console.WriteLine(Indent(2) + "Version 1");
            Console.WriteLine(Indent(4) + "Version 1.1");
            Console.WriteLine(Indent(4) + "Version 1.2");
            Console.WriteLine(Indent(6) + "Version 1.2.1");
            Console.WriteLine(Indent(2) + "Version 2");
            Console.WriteLine(Indent(4) + "Version 2.1");
            Console.WriteLine(Indent(4) + "Version 2.2");
        public static string Indent(int count)
        {
            return "".PadLeft(count);
        }

Output is like below.

Versions
  Version 1
    Version 1.1
    Version 1.2
      Version 1.2.1
  Version 2
    Version 2.1
    Version 2.2