Open In App

How to Pad a String in Java?

Last Updated : 22 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Padding a String in Java involves adding extra characters, which may be spaces, zeros, or other given characters to the beginning or end of a string to meet a desired length. This is normally used in formatting outputs or aligning texts.

Example:

The String.format() method is a simple way to pad strings with spaces.

Java
// Java Program to demonstrate padding a string 
// with spaces using String.format()
public class PadString {
    public static void main(String[] args) {
      
        String s = "Java";

        // Right-pad with spaces
        String r = String.format("%-10s", s);

        // Left-pad with spaces
        String l = String.format("%10s", s);

        System.out.println("'" + r + "'");
        System.out.println("'" + l + "'");
    }
}

Output
'Java      '
'      Java'

Note: This method can only perform left and right padding.

Other Methods to Pad a String in Java

1. Using StringBuilder

We can pad a string using StringBuilder for more flexibility and to pad with custom characters.

Example:

Java
// Java program to demonstrate left-padding 
// a string with a specific character
public class PadString {
    public static void main(String[] args) {
      
        String s = "123";
      
        // desired total length
        int l = 6;             

        // Left pad with zeros
        String p = leftPad(s, l, '0');
        System.out.println("" + p);
    }

    // Method to left-pad a string with a 
    // specified character to a given length
    private static String leftPad(String s, int l, char p) {
      
        // Create a StringBuilder to build the padded string
        StringBuilder sb = new StringBuilder();
      
        // Append the pad character until the 
        // total length matches the required length
        while (sb.length() + s.length() < l) {
            sb.append(p);
        }
      
        sb.append(s);
        return sb.toString();
    }
}

Output
000123


2. Using String.repeat() of Java 11

Java 11 introduced the String.repeat() method and this makes padding a string straightforward.

Example:

Java
// Java Program to demonstrate left-padding 
// a string with a specific character
public class PadString {
    public static void main(String[] args) {
      
        String s = "Java";

        // Left pad with dashes
        // Using the repeat() method to 
        // repeat the dash '-'
        String p = "-".repeat(6 - s.length()) + s;
        System.out.println("" + p);
    }
}

Output
--Java


3. Using Apache Commons Lang

The Apache Commons Lang package provides the StringUtils class, which includes methods for left, center, and right padding of strings.

Note: You need to install this module before running the code, so it may not work on online compilers.

Example:

Java
// Java Program to pad a String using Apache Commons Lang
// Importing StringUtils from Apache Commons Lang
import org.apache.commons.lang3.StringUtils; 
import java.lang.*; 
import java.io.*; 

public class GFG { 

    // Function to perform left padding 
    public static String leftPadding(String s, char ch, int L) { 
        
        // Left pad the string using StringUtils
        String r = StringUtils.leftPad(s, L, ch); 
        return r; 
    } 

    // Function to perform center padding 
    public static String centerPadding(String s, char ch, int L) { 
        
        // Center pad the string using StringUtils
        String r = StringUtils.center(s, L, ch); 
        return r; 
    } 

    // Function to perform right padding 
    public static String rightPadding(String s, char ch, int L) { 
        
        // Right pad the string using StringUtils
        String r = StringUtils.rightPad(s, L, ch); 
        return r; 
    } 
 
    public static void main(String[] args) { 
        String s1 = "GeeksForGeeks"; 
        char ch = '-'; 
        int L = 20; 

        System.out.println(leftPadding(s1, ch, L)); 
        System.out.println(centerPadding(s1, ch, L)); 
        System.out.println(rightPadding(s1, ch, L)); 
    } 
}

Output:

-------GeeksForGeeks
---GeeksForGeeks----
GeeksForGeeks-------

Next Article

Similar Reads