[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


stringbuffer left justify

Posted on 2012-09-19
Medium Priority
Last Modified: 2012-09-19
I am creating a file with mostly static text,
but i had a problem when i want to preserve space.

I wrote the below code for creating file..

StringBuffer bufferText = new StringBuffer();
bufferText .append(String.format("%2s", "AB" ));
bufferText .append(String.format("%4s", "2012"));
bufferText .append(String.format("%1s", " "));
bufferText .append(String.format("%2s", "CD" ));
bufferText .append(String.format("%20s", "Hi Experts"));
bufferText .append(String.format("%2s", "EF" ));

The file was created with the below text

AB2012 CD          HI ExpertsEF

but i want the output to be left justified...like below...

AB2012 CDHI Experts          EF

I mean in the below line of code the string length is 20 but the string "HI Experts" is just 10 so the remaining 10 characters are spaces... i want those ten characters to be after "HI Experts" not before "HI Experts"

bufferText .append(String.format("%20s", "Hi Experts"));

I see that stringbuffer by default appends the string with right justification can i change it to left justification....

is there a way to achieve this... other than me specifying
bufferText .append(String.format("%20s", "HI Experts          "));
i dont want to do it in the above method because sometimes the string length can be 200 and my actual string can be only 20 characters... so i dont want to count 180 spaces...
Question by:shragi
  • 3
  • 2
LVL 86

Accepted Solution

CEHJ earned 2000 total points
ID: 38414982
bufferText .append(String.format("%-20s", "Hi Experts"));

Open in new window

Use StringBuilder btw, unless you've a good reason not to

Author Comment

ID: 38415014
that solved my problem.... i ahve another question...how to add only spaces... using append...

i mean if i want to add some 20 spaces i am using the below line
bufferText .append(String.format("%-20s", "                    "));

is there any other way to do this...
LVL 86

Expert Comment

ID: 38415046
You don't need to add spaces - that's the whole point of formatting. An empty String will suffice

Author Comment

ID: 38415103
excellent thank you :)
LVL 86

Expert Comment

ID: 38415362

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question