Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust trailing whitespace?

Tags:

java

I am writing to a file in java but the strings that are input into the file are different, how do i adjust the trailing whitespace depending on the length of the string.

for example

First Name     Last Name     Address
----------     ---------     -------
Michael        Jordan        23 E. Jump Street
Larry          Bird          33 North Celtics Run
like image 586
Mike Avatar asked Dec 04 '22 07:12

Mike


1 Answers

You can use String.format():

System.out.println(String.format("[%-20s]", "foo"));

will give you:

[foo                 ]
like image 153
Michał Šrajer Avatar answered Dec 19 '22 14:12

Michał Šrajer