wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Padding Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Padding Strings" Watch "Padding Strings" New topic
Author

Padding Strings

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi guys,

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 ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

how about this?

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
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.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
Now that was a good conversation.
Thank You


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
I wrote pad methods that I included in my StringHelper class:
http://ostermiller.org/utils/StringHelper.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Padding Strings
 
Similar Threads
convert uft-8 into ascii format
String and enhanced for-loop
Null teminating a string
how can i convert char [] to unsigned char [] to connect to an app writen in 'C'
Wrong encoding of the data from POST