Javatpoint Logo
Javatpoint Logo

How to pad a String in Java

Padding a string in Java refers to the practice of appending specific characters, typically spaces or a chosen character, to the start, end, or both sides of a string. The technique is employed to reach a specified length or to align text uniformly. It's a technique frequently used in multiple contexts, including organizing text in tabular formats, aligning content in documents or reports, or improving the overall appearance of textual output.

Example 1:

Input:

String str1 = "Java";

char ch1 = '-';

int L1 = 10;

Output:

Left Padding: "------Java"

Center Padding: "---Java----"

Right Padding: "Java------"

Example 2:

Input:

String str2 = "123";

char ch2 = '*';

int L2 = 8;

Output:

Left Padding: "*****123"

Center Padding: "**123***"

Right Padding: "123*****"

Approach: Using String format() method

The String.format() method in Java returns a formatted string using the given locale, specified format string, and arguments. The method allows for creating strings with placeholders replaced by formatted values.

Syntax:

  • format: A format string that contains placeholders for the arguments. Placeholders are denoted by % followed by a format specifier. For example, %s is used for strings, %d for integers, %f for floating-point numbers, and so on.
  • arg1, arg2, ...: The arguments to be formatted and substituted into the placeholders in the format string.

Algorithm:

Step 1: Set the original string (originalString) to "Hello" and also define the desired length for padding (desiredLength) as 10.

Step 2: Call the padRight method with the original string, desired length, and the padding character '_' to get the right-padded string.

Step 3: Call the padLeft method with the original string, desired length, and the padding character '_' to get the left-padded string.

Step 4: Create a right-padded string by left-justifying `input` with `String.format`, replacing spaces with `paddingChar`, and returning it.

Step 5: Left-pad `input` by right-justifying it using `String.format`, replacing spaces with `paddingChar`, and returning the result.

Implementation:

Filename: StringPaddingExample.java

Output:

Padded Right: 'Hello_____'
Padded Left: '_____Hello'

Approach: Using Apache Common Lang

Apache Commons Lang's StringUtils class provides convenient methods such as leftPad(), center(), and rightPad() for easily padding strings on the left, center, and right, respectively. These methods are valuable for aligning text within a specified length by adding padding characters. The functionality is commonly used in scenarios like formatting output, designing user interfaces, and aligning data in reports. The simplicity and effectiveness of these methods contribute to improved code readability and presentation in Java applications.

Algorithm:

Step 1: Initialize a string originalString and an integer desiredLength.

Step 2: Call padLeft with originalString, desiredLength, and a padding character, storing the result in leftPadded. Print leftPadded.

Step 3: Call padCenter with originalString, desiredLength, and a padding character, storing the result in centerPadded. Print centerPadded.

Step 4: Call padRight with originalString, desiredLength, and a padding character, storing the result in rightPadded. Print rightPadded.

Step 5: Implement `StringUtils.leftPad` to append the specified padding character to the beginning of the input string until it reaches the desired length, then return this left-aligned string.

Step 6: Utilize `StringUtils.center` to position the input string in the middle of a field with the set length, filling the space on both sides with the chosen character, and return the centrally aligned string.

Step 7: Apply `StringUtils.rightPad` to add the selected padding character to the end of the input string until it attains the specified length, and return this right-aligned string.

Implementation:

Filename: StringUtilsExample.java

Output:

Left Padded: '__________Hello'
Center Padded: '______Hello______'
Right Padded: 'Hello____________'






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA