2

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

2 Answers 2

1

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            
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest to use

StringUtils.rightPad("String1",15)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.