String Formatting in C# to add padding on the right

CsharpProgrammingServer Side Programming

To add padding on the right to a string −

const string format = "{0,10}";

Now add it to the string −

string str1 = string.Format(format, "Marks","Subject");

Let us see the complete code −

Example

 Live Demo

using System;

public class Program {
   public static void Main() {

      // set right padding
      const string format = "{0,10}";
      string str1 = string.Format(format, "Marks","Subject");
      string str2 = string.Format(format, "95","Maths");

      Console.WriteLine(str1);
      Console.WriteLine(str2);
   }
}

Output

Marks
95
raja
Published on 31-Aug-2018 13:16:55

Advertisements