StringUtils.rightPad() Method

StringUtils.rightPad() Method | The rightPad() method is a static method in a StringUtils class that helps to add the pad spaces/character/string to the right side of the string. There are three variations in this method to right pad a string with space, character, and string.

StringUtils rightPad() Method

The syntax for the rightPad() method and its variations are as follows:-

  1. public static String rightPad(final String str, final int size):- Right pad a String with spaces (‘ ‘). The String is padded to the size.
  2. public static String rightPad(final String str, final int size, final char padChar):- Right pad a String with a specified character. The String is padded to the size.
  3. public static String rightPad(final String str, final int size, String padStr):- Right pad a String with a specified String. The String is padded to the size.

Parameters:-
str: the string needed to add spaces/character/string.
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 padded string.

StringUtils.rightPad() Method Example

Let us see an example for padding whitespaces to the string using the public static String rightPad(final String str, final int size) method.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Calendar";
        System.out.println("String: \"" + string + "\"");
        System.out.println("Length of the string before padding: " + string.length());

        int num = 20;
        String string1 = StringUtils.rightPad(string, num);
        System.out.println("New String: \"" + string1 + "\"");
        System.out.println("Length of the string after padding: " + string1.length());
    }
}

Output:-


String: “Calendar”
Length of the string before padding: 8
New String: "Calendar "
Length of the string after padding: 20

In the above example initially, the string contains “Calendar” but after calling rightPad(string, 20), twelve spaces are added to the right side of the string and hence its length becomes 20.

What happens if the given size value is lesser than the length of the string? In that case, the string will not be padded and the original string itself will be returned. The below example demonstrates it:-

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Calendar";
        System.out.println("String: \"" + string + "\"");
        System.out.println("Length: " + string.length() );

        // 3 < string.length()
        String string1 = StringUtils.rightPad(string, 3);
        System.out.println("New String: \"" + string1 + "\"");
        System.out.println("Length: " + string1.length());
    }
}

Output:-

String: “Calendar”
Length: 8
New String: “Calendar”
Length: 8

Java rightPad() StringUtils Example

This is the example for the second variation:- public static String rightPad(final String str, final int size, final char padChar) Here instead of space it will pad the string with a given character.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Calendar";
        System.out.println("String: \"" + string + "\"");
        System.out.println("Length of the string before padding: " + string.length());

        int num = 20;
        String string1 = StringUtils.rightPad(string, num, 'A');
        System.out.println("New String: \"" + string1 + "\"");
        System.out.println("Length of the string after padding: " + string1.length());
    }
}

Output:-

String: “Calendar”
Length of the string before padding: 8
New String: “CalendarAAAAAAAAAAAA”
Length of the string after padding: 20

Java StringUtils rightPad() Example

This is an example of the third variation:- public static String rightPad(final String str, final int size, String padStr) Here a substring will be padded to the given string.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Calendar";
        System.out.println("String: \"" + string + "\"");
        System.out.println("Length of the string before padding: " + string.length());

        String string1 = StringUtils.rightPad(string, 18, "Cal");
        System.out.println("New String: \"" + string1 + "\"");
        System.out.println("Length of the string after padding: " + string1.length());
    }
}

Output:-

String: “Calendar”
Length of the string before padding: 8
New String: “CalendarCalCalCalC”
Length of the string after padding: 18

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 *