StringUtils leftPad() Example

StringUtils leftPad() Example | In the StringUtils class, we have a method called leftPad() which is used to add whitespaces or given characters/strings to the left of the string. There are three different variations of this method as listed below. The syntax for the leftPad() method and its variations are as follows:-

  • public static String leftPad(final String str, final int size)
  • public static String leftPad(final String str, final int size, final char padChar)
  • public static String leftPad(final String str, final int size, String padStr)

Parameters:
string: the string needed to add spaces or zeros
size: it is an integer value, which should say the final length of the string after padding.
padChar: the character which needs to be padded.
padStr: the string which needs to be padded.
Return type: String.
Returns: the string after padding.

Apache StringUtils leftPad() Example-1

This example is to demonstrate the working of public static String leftPad(final String str, final int size).

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "Books";
        int number = 19;
        System.out.println("Length of the string before padding: " + str.length());
        String str1 = StringUtils.leftPad(str, number);
        System.out.printf("StringUtils.rightPad(%s, %s) = %s", str, number, str1);
        System.out.println("\nLength of the string after padding: " + str1.length());

    }
}

Output:-

Length of the string before padding: 5
StringUtils.leftPad(Books, 19) = Books
Length of the string after padding: 19

StringUtils leftPad() Example-2

This is to demonstrate the working of the public static String leftPad(final String str, final int size, String padStr) method.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "Books";
        int number = 19;
        System.out.println("Length of the string before padding: " + str.length());
        String str1 = StringUtils.leftPad(str, number, "Something");
        System.out.printf("StringUtils.rightPad(%s, %s) = %s", str, number, str1);
        System.out.println("\nLength of the string after padding: " + str1.length());
    }
}

Output:-

Length of the string before padding: 5
StringUtils.rightPad(Books, 19) = SomethingSometBooks
Length of the string after padding: 19

Java StringUtils leftPad() Example-3

This code demonstrates the working of the public static String leftPad(final String str, final int size, final char padChar) method.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "Books";
        int number = 19;
        System.out.println("Length of the string before padding: " + str.length());
        String str1 = StringUtils.leftPad(str, number, 'A');
        System.out.printf("StringUtils.leftPad(%s, %s) = %s", str, number, str1);
        System.out.println("\nLength of the string after padding: " + str1.length());
    }
}

Output:-

Length of the string before padding: 5
StringUtils.leftPad(Books, 19) = AAAAAAAAAAAAAABooks
Length of the string after padding: 19

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *