Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: How can I w...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 13 Topic 16039 of 16590
Post > Topic >>

Re: How can I write right-justified into a 12 chars width field?

by alexandre_paterson@[EMAIL PROTECTED] Apr 29, 2008 at 08:21 PM

On Apr 29, 7:23 pm, grem...@[EMAIL PROTECTED]
 (Catherina Svensson) wrote:
> Lets say I have a String var which can contain values of different
lengths e.g.
> "aaa"  or "bb bb bb".
>
> I want to print them out with System.out.println right-justified with a
field width
> of 12 chars. If necessary this field should be left padded with blanks.
> For the samples above the output should be e.g.:
>
> "         aaa"
> and
> "    bb bb bb"
>
> How can I implement this right-justified, blank padded, fixed-width
filed output in Java?

What if the field is bigger than 12 chars?

Anyway Jakarta commons contains a lot of very useful stuff, including
a lot of StringUtils
very convenient methods, including leftpad, etc.

If you want don't want to use Jakarta commons for this, you may write
a helper method
somewhere in your project.

The following may help (not tested):

    private static final String PAD = "            ";

    final String s ="aa aaa aa";
    System.out.println( PAD.substring( Math.min(PAD.length(),
s.length())) + s);
 




 13 Posts in Topic:
How can I write right-justified into a 12 chars width field?
gremlin@[EMAIL PROTECTED]  2008-04-29 18:23:11 
Re: How can I write right-justified into a 12 chars width field?
Lord Zoltar <lord.zolt  2008-04-29 11:30:08 
Re: How can I write right-justified into a 12 chars width field?
Mark Space <markspace@  2008-04-29 14:03:30 
Re: How can I write right-justified into a 12 chars width field?
Knute Johnson <nospam@  2008-04-29 14:42:26 
Re: How can I write right-justified into a 12 chars width field?
=?ISO-8859-1?Q?Arne_Vajh=  2008-04-29 19:12:28 
Re: How can I write right-justified into a 12 chars width field?
Christian <fakemail@[E  2008-04-30 01:42:37 
Re: How can I write right-justified into a 12 chars width field?
Chase Preuninger <chas  2008-04-29 18:25:40 
Re: How can I write right-justified into a 12 chars width field?
petersprc <petersprc@[  2008-04-29 19:13:04 
Re: How can I write right-justified into a 12 chars width field?
Andrea Francia <andrea  2008-04-30 21:11:43 
Re: How can I write right-justified into a 12 chars width field?
alexandre_paterson@[EMAIL  2008-04-29 20:21:46 
Re: How can I write right-justified into a 12 chars width field?
Roedy Green <see_websi  2008-04-30 05:04:11 
Re: How can I write right-justified into a 12 chars width field?
Donkey Hot <spam@[EMAI  2008-04-30 16:48:13 
Re: How can I write right-justified into a 12 chars width field?
thufir <hawat.thufir@[  2008-04-30 22:08:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 14:08:35 CST 2008.