1. Introduction

String padding in Java is a common operation where additional characters are added to the beginning or end of a string, making it a specified length. This can be particularly useful when formatting output or aligning text. In this guide, we’ll look at how to perform string padding in Java using the String.format() method and the StringUtils.leftPad() and StringUtils.rightPad() methods from the Apache Commons Lang library.

2. Program Steps

1. Define a string that needs to be padded.

2. Use String.format() for left and right padding with spaces.

3. Include Apache Commons Lang library and use StringUtils.leftPad() and StringUtils.rightPad() for padding with specific characters.

4. Print the padded strings.

3. Code Program

// Import the StringUtils class from Apache Commons Lang library
import org.apache.commons.lang3.StringUtils;

public class StringPadding {

    public static void main(String[] args) {
        // Step 1: Define a string that needs to be padded
        String originalString = "Java";

        // Step 2: Use String.format() for left and right padding with spaces
        String leftPaddedString = String.format("%10s", originalString);
        String rightPaddedString = String.format("%-10s", originalString);

        // Print the padded strings with spaces
        System.out.println("Left Padded with spaces : " + leftPaddedString + ":");
        System.out.println("Right Padded with spaces: " + rightPaddedString + ":");

        // Step 3: Include Apache Commons Lang library and use StringUtils.leftPad() and StringUtils.rightPad() for padding with specific characters
        String leftPaddedWithChars = StringUtils.leftPad(originalString, 10, '*');
        String rightPaddedWithChars = StringUtils.rightPad(originalString, 10, '*');

        // Step 4: Print the padded strings with specific characters
        System.out.println("Left Padded with chars  : " + leftPaddedWithChars + ":");
        System.out.println("Right Padded with chars : " + rightPaddedWithChars + ":");
    }
}

Output:

Left Padded with spaces :       Java:
Right Padded with spaces: Java      :
Left Padded with chars  : ******Java:
Right Padded with chars : Java******:

4. Step By Step Explanation

– In Step 1, we define the originalString that we intend to pad.

Step 2 demonstrates using String.format() for left and right padding with spaces. The %10s and %-10s format specifiers are used for left and right padding, respectively. The 10 in the format specifier indicates the width to which the string should be padded.

– In Step 3, we use StringUtils.leftPad() and StringUtils.rightPad() methods from the Apache Commons Lang library for padding with specific characters. These methods take the original string, the final length of the string after padding, and the character to pad with as parameters.

– Finally, in Step 4, we print out all the padded strings. The output clearly shows the difference in padding on both left and right, using spaces and specific characters.