aspose file tools
The moose likes Beginning Java and the fly likes Padding a string to 5 digits Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Padding a string to 5 digits" Watch "Padding a string to 5 digits" New topic
Author

Padding a string to 5 digits

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
I have a numeric string that I want to make sure is 5 digits long. Meaning
if the number is 1234, I want to make it 01234. What is the easist way to
accomplish this?

Thanks/
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
Maybe you should take a look at the DecimalFormat class.


-Rich, SCJP 1.4
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's a little cheaper to say

String s = "00000" + input;
s = s.substring(input.length());

Try it for a few different lengths. That may be "off by one".


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
"Cheaper" in what sense? Performance, I presume. However I think it's pretty rare that a method like this is a significant bottleneck. Particularly since formatting a string is typically a prelude to writing it somewhere, and most IO operations will probably take significantly longer than the formatting anyway. I think the main thing to look for here is something that's easy to read, easy to debug, and easy to modify when requirements change. I would suggest that now that J2SE 5 is out, the preferred way to do this is with a format string, e.g:

The "%5d" is a format string, which indicates a decimal number formatted to a length of five spaces, padded in frot with zeros. There's a bit of a learning curve to understand format strings initially, but I think it's time well spent. With a little practice you can read them fairly easily, and you have access to tremendous flexibility in how you format things. Here's another sample of their use. Run it and see what you get:

Here are some articles discussing formatters:

http://www.opensourcetutorials.com/tutorials/Server-Side-Coding/Java/formatted-output/page1.html
http://www.devx.com/Java/Article/20359/0/page/4
http://www.developer.com/java/other/article.php/3327721
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter09/FormatterPrintfConsole.html
[ July 26, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Also for many common String operations have a look at Jakarta Commons lang StringUtils class.

An added advantage is that most of the methods in StringUtils are null safe, if that meets your requirements.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Cheaper in CPU cycles / performance, yes. Substringing and concatenating was about 100x faster than two SimpleDateFormats and a Date in a routine that converted MM/DD/YYYY to YYYYMMDD for us. Of course you have to do a million or so in a row to notice the difference, so as usual write for humans to understand first, optimize if you prove it's a problem.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
We're still working with J2SE 1.4 here, so I wrote the following function:



There are probably much better ways.

-Yuriy
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The nice thing about that solution is that you can generalize it to pad any string with any character. You might be able to shave some milliseconds off by computing the length of pad you'll need and use Arrays.fill() or something. Again: readable first, milliseconds later.

The REXX language calls this function right() because it right justifies. It also truncates if the string is too long to start with, which is perhaps correct (or not) if you're writing columnar reports to a text file or console. The REXX string and word functions are very sensible; worth looking at as inspiration before you embark on writing a library of your own.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Padding a string to 5 digits
 
Similar Threads
Writing numbers
Error from sudoku class
NumberFormat parsing currency value failure
Questions from K&B 6
How to make a regular expression that the order doesn't matter