How to pad string from right using String.PadRight() in C#?

String.PadRight() Method of String class in C#: Here, we will learn how to pad string from right with specific characters in C#?

Given a string and we have to pad the string from right with specific characters.


Method returns padded string from right.


String String.PadRight(int totalLength, char ch);

totalLength : This parameter specifies total length of string after padding.
ch : This parameter specifies a character which will be used to pad string from right side.


Input string: "This is a sample string"
Padding string with '#' and total string length will be 30
Output string: "This is a sample string#######"

Explanation: Input string's length was 23 and to make it 30, program added 7 characters (#) in the end.

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
    class Program
        static void Main()
            String str1 = "This is a sample string"; ;
            String str2;
            str2 = str1.PadRight(30, '#');

            Console.WriteLine("String after right padding:(" + str2+")");



String after right padding:(This is a sample string#######)

In above program we used ‘#’ character to pad string from right side, and string length after padding will be 30 characters.

C# Basic Programs »



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

© some rights reserved.