
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# String - PadLeft() Method
The C# String PadLeft() method returns a new string of a specified length, with the beginning of the current string padded with space or a specified Unicode character (i.e., the string is right-aligned by padding it with space or the specified character on the left).
Syntax
Following are the syntax of the C# string PadLeft() method −
Default Syntax
Returns a new string that right-aligns the characters in the current string by padding them with spaces on the left by the specified length.
public string PadLeft (int totalWidth);
Parametrised Syntax
Returns a new string that right-aligns the characters in the current string by padding them with a specified character on the left by the specified length.
public string PadLeft (int totalWidth, char paddingChar);
Parameters
This method accepts the following parameters −
- totalWidth: The number of character in the resulting string, including the original string and any padding characters.
- paddingChar: It is an optional parameter that represent a padding character.
Return Value
This method returns a new string. That is equivalent to the current string, but right-aligned and padded on the left with spaces or characters.
Example 1: Default Padding with Space
Following is a basic example of the PadLeft() method to align the string right by padding with space on the left by a specified length −
using System; class Program { static void Main() { string original = "tutorialspoint"; string padded_left = original.PadLeft(20); Console.WriteLine($"'{padded_left}'"); } }
Output
Following is the output −
' tutorialspoint'
Example 2: Padding with Custom Character
Let's look at another example. Here, we use the PadLeft() method to align the string right by padding with specified character on the left −
using System; class Program { static void Main() { string original = "Tutorialspoint"; string padded_left = original.PadLeft(20, '-'); Console.WriteLine($"'{padded_left}'"); } }
Output
Following is the output −
'------Tutorialspoint'
Example 3: What if Total Length is Small than String
The below example shows how the PadLeft() method works if the total length is smaller than the string. If totalWidth is less than the length of the current string, the method returns a reference to the existing string −
using System; class Program { static void Main() { string original = "Tutorialspoint"; string padded_left = original.PadLeft(13, '-'); Console.WriteLine($"'{padded_left}'"); } }
Output
Following is the output −
'Tutorialspoint'