Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String padLeft() method



Pad the String with the spaces appended to the left.This method has 2 different variants.

  • String padLeft(Number numberOfCharacters) − Pad the String with the spaces appended to the left.

Syntax

String padLeft(Number numberOfCharacters)

Parameters

numberOfCharacters − The number of characters to pad the string with.

Return Value

The new value of the string with the padded characters

  • String padLeft(Number numberOfCharacters, String padding) − Pad the String with the padding characters appended to the left.

Syntax

String padLeft(Number numberOfCharacters, String padding)

Parameters

  • numberOfCharacters − The number of characters to pad the string with.

  • Padding − The character to apply for the padding.

Return Value

The new value of the string with the padded characters.

Example - Usage of padLeft(int) method

Following is an example of the usage of padLeft(int) method −

Example.groovy

class Example { 
   static void main(String[] args) { 
      String a = "Hello World";
		
      println(a.padLeft(14)); 
      println(a.padLeft(16)); 
   } 
}

Output

When we run the above program, we will get the following result −

   Hello World
     Hello World

Example - Usage of padLeft(int, char) method

Following is an example of the usage of padLeft(int, char) method −

Example.groovy

class Example { 
   static void main(String[] args) { 
      String a = "Hello World";
		
      println(a.padLeft(14,'*')); 
      println(a.padLeft(16,'*')); 
   } 
}

Output

When we run the above program, we will get the following result −

***Hello World
*****Hello World

Example - Usage of padLeft(int, char) method on String literals

Following is an example of the usage of padLeft(int, char) method −

Example.groovy

class Example { 
   static void main(String[] args) { 		
      println("Hello World".padLeft(14,'*')); 
      println("Hello World".padLeft(16,'*')); 
   } 
}

Output

When we run the above program, we will get the following result −

***Hello World
*****Hello World
groovy_strings.htm
Advertisements