String Formatting in C# to add padding on the right


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

Updated on: 22-Jun-2020

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements