i have a string, for example "127.0.0.1", which i need in this form: "127.000.000.001" (filled up with zero).
right now i'm working with a loop and a set of conditions, but i'm sure there is a more elegant (=efficent) way. i played with MaskFormatter("###.###.###.###"), but I'm not sure if this solves my problems. Even if i need this conversion for a text field, I don't want to be too close to Swing, I'd like to solve this problem in a more general way...
127.000.000.001 isn't a number in any system I know. That's about as good as you're going to do for formatting a rather complex string. As an exercise see if you can make your pad routine about half as long as the one linked above. That part is reusable as a bonus.
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
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: