Padding Strings

If I want my Strings to be at *least* 8 characters long, I have to pad any string shorter than this with either leading or trailing blanks.

I've thought of a number of ways to do this, but choose the following. Do you think this is the best way? Or have I missed something more efficient?(there are 8 spaces in that blank string above). [ February 25, 2002: Message edited by: Mike Curwen ]

That's about as simple as you can make it in terms of coder efficiency - it's simple to write and hard to screw up. Since this is posted in Beginner, most readers should just stop there and ignore the rest of this post. But since you asked, I'll continue my answer anyway. From a performance perspective, I think it can be improved a little bit. There's at least one unneccessary String object being created, and the final String being created consumes slightly more memory than it needs to. For speed, substring() returns a new String which internally uses the same char[] array as the parent String. Which means that even though the parent String can be gc'd, the char[] array cannot. This might be significant if you're storing a large number of strings created this way, as the char[] arrays will average 50% more space than they need. As an alternative, here's one of the methods in my StringUtil class. There's a padRight() as well, plus versions that use a space as the default character for padding.

There's room for a bit more optimization here. Rather than appending each char individually, it would be faster to append from a char[] pre-loaded with the pad character. Alternately, it may be better to forego StringBuffer entirely, and use a char[] array instead. But there's a little inefficiency at the end as new String(char[]) will make a copy of the array, while StringBuffer's toString() will reuse its array internally. For most applications, the optimizations discussed here won't really matter. But occasionally they will. If you do a lot of this operation, it can't hurt to have a nice efficient method for it, just in case.

The immutable string literal " " is always going to be around, so no memory loss is associated with it. The extra String returned by substring() is gc()'d as well, so its char[] is gc()'d . Also, the original char[] underlying the 'first' value of the String level is gc()'d. Have I missed anything? (aside from the dubious non-optimization of calling .length() twice, without assigning it to a separate int?)

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

posted Feb 25, 2002 16:32:00

0

Yes, I think that's about as good as it gets. As long as you know in advance what the pad size is, and what character to pad with, you're golden. Incidentally, the char[] underlying the substring() is really the same one that underlies the parent string " " - so that's one less object to create and destroy.