MENU

Shaped by Java LPAD and RPAD

TOC

Formatting with Java LPAD and RPAD. Fill in the specified characters on the left and right of the string.

Javaand then use the SQL functionLPAD, ,RPADHere is a sample source that performs the same process as
These arefixed length formatThis is very useful when dealing with

  • LPAD... Returns a string with a repeated string filled in on the left side of the string so that the total number of characters is the specified number of digits.
  • RPAD... Returns a string with a repeated string filled in on the right side of the string so that the total number of characters is the specified number of digits.


sample program


Execution Result

◆Example of Execution

public static void main(String[] args) {
    // For lpad, fill in the spaces to the left until there are 5 digits.
    String ret = lpad("abc", 5 , " ");
    System.out.println("'" + ret + "'");

    // For rpad. fill space to the right until 5 digits.
    ret = rpad("abc", 5 , " ");
    System.out.println("'" + ret + "'");

    // If null
    ret = rpad(null, 5 , " ");
    System.out.println("'" + ret + "'"); // if null
}

◆Output result

'abc'
' 'abc '
' '
TOC