pvgoddijn pvgoddijn - 1 year ago 129
Java Question

How can I pad a String in Java?

Is there some easy way to pad Strings in Java?

Seems like something that should be in some StringUtil-like API, but I can't find anything that does this.

Answer Source

Apache StringUtils has several methods: leftPad, rightPad, center and repeat. https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html


As others have mentioned and demonstrated in this answer, String.format() and the Formatter classes in the JDK are better options. Use them over the commons code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download