Home » .Net

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.

String.PadRight()

Method returns padded string from right.

Syntax:

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

Here,
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.

Example:

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+")");

        }
    }
  
}

Output

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




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.