If you're using JDK 5, then yes there is another (better?) way - using a
java.util.Formatter. This is a little hard to figure out how to use at first, but it's well worth the effort, I think, as it's very powerful and flexible.
Note that you usually don't need to create a Formatter directly. There are a number of convenience methods built into preexisting classes:
java.lang.String.format()
java.io.PrintStream.format()
java.io.PrintStream.printf()
java.io.PrintWriter.format()
java.io.PrintWriter.printf()
For example to pad a number with zeros to 3 digits, you can use:
String output = String.format("%03d", Integer.parseInt(input));
[ September 09, 2005: Message edited by: Jim Yingst ]