stringbuffer left justify

Posted on 2012-09-19
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 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
    LVL 86

    Accepted Solution

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

    Open in new window

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

    Author Comment

    that solved my problem.... i ahve another 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

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

    Author Comment

    excellent thank you :)
    LVL 86

    Expert Comment


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now