Groovy – padLeft()

  • Post author:
  • Post category:Groovy
  • Post comments:1 Comment
left

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

Following is an example of the usage of both method variants โˆ’

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

When we run the above program, we will get the following result โˆ’

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

Previous Page:-Click Here

This Post Has One Comment

Leave a Reply