user3001301
user3001301

Reputation: 125

Java set way to give certain amount of spaces in output?

How do I output a string but each part of the output can have a certain amount of characters? So say the first string gets 15 characters than the next gets another 15 and the same for the one after that?

Output

Output1          Output2          Output3
-------------------------------------------

String1          String2          String3

rather than where if String1000 has more characters than String1 it won't disrupt the outputs length

String1000            String2            String3

Upvotes: 2

Views: 278

Answers (2)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

You can use the C style printf in Java also. Here it is:

String x = "someString", y = "moreStringsss", z = "end";
System.out.printf("%-15s%-15s%-15s\n", x,y,z); 

It outputs:

someString     moreStringsss  end            

Upvotes: 1

SWoeste
SWoeste

Reputation: 1167

I would suggest to use

StringUtils.rightPad("String1",15)

from the Apache Commons Library (JavaDoc) and concatenate the result at your convenience.

Upvotes: 0

Related Questions