Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Padding a string to 5 digits  RSS feed

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should take a look at the DecimalFormat class.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!