Home » C#

C# | Right padding a string with spaces

Example of String.RightPad() method in C#: Here, we are going to learn how to right pad a string with spaces in C#?
Submitted by IncludeHelp, on November 15, 2019

RightPad() method is a library method of the String class. It is used to pad the string from the right side with spaces.

Syntax:

    string string.PadRight(int totalWidth);

Here, totalWidth is the total number of characters of the string, if it is more than the length of the string, string will be padded with spaces, method returns the space padded string.

Program:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Hello world";

            Console.WriteLine("Demonstration of PadRight method");
            Console.WriteLine(str.PadRight(str.Length + 1) + "##########");
            Console.WriteLine(str.PadRight(str.Length + 2) + "##########");
            Console.WriteLine(str.PadRight(str.Length + 3) + "##########");
            Console.WriteLine(str.PadRight(str.Length + 4) + "##########");
            Console.WriteLine(str.PadRight(str.Length + 5) + "##########");
            
            Console.WriteLine();  
        }
    }
}

Output

Demonstration of PadRight method
Hello world ##########
Hello world  ##########
Hello world   ##########
Hello world    ##########
Hello world     ##########
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.