mowienay mowienay - 7 months ago 14
Java Question

How to apply padding on a Java String object

To ensure a fixed length between the last characters of two consequent strings

String1 String2q
S1 String222


The java language provides what you need in String.format()

String padded = String.format("%20-s", s);  

This will print a string and then add spaces after it so it is always length 20.

You can either use this method or just put the format directly into your output code.

If you really want to always leave exactly 20 characters (which is not what your example shows) just define a string constant and use that:

private static final String PAD = "                ";