String padding

In this chapter you will learn:

  1. How to pad a string
  2. Pad with space

Padding string

Padding string is to add specified character to a string to a given length.

We can choose the pad left or right.

using System;/*j  a  va  2  s.com*/

class Sample
{
    public static void Main()
    {
        string s = "java2s.com";

        s = s.PadLeft(20,'=');
        Console.WriteLine(s);

        s = s.PadRight(40,'+');
        Console.WriteLine(s);
    }
}

The output:

Pad with space

By default the Pad functions add space to strings.

using System;//from j a va 2s.co  m

class MainClass
{

  public static void Main()
  {
    string myString = "java2s.com";   
    
    string myString14 = myString.PadLeft(20);
    Console.WriteLine(myString14);

  }

}

Next chapter...

What you will learn in the next chapter:

  1. A string can control a switch statement
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String