Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to pad a string on both sides in Java

4 Answers

0 votes
public class PadString {
    public static String padCenter(String input, char padChar, int totalLength) {
        if (input.length() >= totalLength) return input;

        int paddingNeeded = totalLength - input.length();
        int leftPadding = paddingNeeded / 2;
        int rightPadding = paddingNeeded - leftPadding;

        StringBuilder padded = new StringBuilder();
        for (int i = 0; i < leftPadding; i++) padded.append(padChar);
        padded.append(input);
        for (int i = 0; i < rightPadding; i++) padded.append(padChar);

        return padded.toString();
    }

    public static void main(String[] args) {
        String original = "Java";
        String padded = padCenter(original, '*', 11);
        
        System.out.println("Padded string: " + padded);
    }
}


/*
run:

Padded string: ***Java****

*/

 



answered Jul 5, 2025 by avibootz
0 votes
class PadString {
    public static void main(String[] args) {
        String str = "Java";
        int totalLength = 11; // Desired total length

        // Calculate left padding
        int paddingLeft = (totalLength - str.length()) / 2;

        // Create the padded string
        String padded = String.format("%" + (paddingLeft + str.length()) + "s", str);
        padded = String.format("%-" + totalLength + "s", padded);

        System.out.println("Padded string: [" + padded + "]");
    }
}


/*
run:

Padded string: [   Java    ]

*/

 



answered Jul 5, 2025 by avibootz
0 votes
class PadString {
    public static void main(String[] args) {
        String str = "Java";
        int totalLength = 11; // Desired total length

        // Calculate left padding
        int paddingLeft = (totalLength - str.length()) / 2;

        // Format with spaces first
        String padded = String.format("%" + (paddingLeft + str.length()) + "s", str);
        padded = String.format("%-" + totalLength + "s", padded);

        // Replace all spaces with '*'
        padded = padded.replace(' ', '*');

        System.out.println("Padded string: " + padded);
    }
}



/*
run:

Padded string: ***Java****

*/

 



answered Jul 5, 2025 by avibootz
0 votes
class PadString {
    public static void main(String[] args) {
        String str = "Java";
        int totalLength = 11; // Desired total length
        char padChar = '*';

        String padded = padString(str, totalLength, padChar);
        System.out.println("Padded string: " + padded);
    }

    public static String padString(String str, int totalLength, char padChar) {
        int paddingLeft = (totalLength - str.length()) / 2;
        String padded = String.format("%" + (paddingLeft + str.length()) + "s", str);
        padded = String.format("%-" + totalLength + "s", padded);
        
        return padded.replace(' ', padChar);
    }
}



/*
run:

Padded string: ***Java****

*/

 



answered Jul 5, 2025 by avibootz
edited Jul 5, 2025 by avibootz

Related questions

1 answer 79 views
1 answer 69 views
1 answer 64 views
1 answer 62 views
1 answer 69 views
1 answer 71 views
...