Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Java aligment

Recommended Posts

Posted · Original PosterOP


i have just one question about aligment in PRINTF() in java. How could i align given text in center?

For example I want to print text like this:

##    This is test    ##

how could I center align whole text? So "This is test" is in the middle? I know for right and left aligment but I need center.



Link to post
Share on other sites

As long as you don't have a fixed column / line length you can't center text. However, if you know the line length you can use StringUtils.center from the Commons Lang library.

System.out.println(StringUtils.center("TEST", 24));
Output is the centered text and 10 whitespaces in the left and right: "          TEST          "

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now