• Post Reply Bookmark Topic Watch Topic
  • New Topic

Padding a String  RSS feed

 
John Daniel
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know about the trim string method which will remove trailing spaces from a string. What I want to know is there an easy way to do the reverse, that is add spaces to the end of a string so that it is a minimum length? The best I've been able to come up with is the following for loop.

for (int i = myStr.length(); i < MIN_LEN; i++) {
myStr += " ";
}

Is there a more straight forward way to perform this function?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't know that this is more straight forward or better, just different

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's about it. You might shave off a few cycles by computing the number of pad characters you need, create and fill an array, then, aw heck, why bother. The loop makes perfect sense. Computing the number you need might be a shade more efficient than testing the length every time around.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're lazy, you can use jakarta commons-lang containing StringUtils class which will do the work for you
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hmmm... this response may go beyond the scope of what's appropriate to the beginner forum. So feel free to skip it if you want. But... using String concatenation in a loop like this is usually a bad idea. There are some exceptions to that statement, but this isn't really one of them. Specifically, if you're repeatedly appending (or prepending) content onto some base content that grows with each iteration - you're probably better off using a StringBuffer, or in JDK 1.5+, a StringBuilder. These allow you to accumulate the growing content in a single place. Using String concatenation with the + operator, you're needlessly wasting time copying the content from one string to another to another to another, each time you go through the loop. Try something like this:

This may be a little longer to write, but in general it's a much more efficient. It is often said that "premature optimization is the root of all evil", but in my opinion, this is one of the exceptions to that principle, where you're better off learning a sensible idom in the first place (using a StringBuffer or StringBuilder) rather than learning bad habits just because they look simpler than good habits.

Alternately, if you're using JDK 1.5+, you can also do this in one line with String.format() or similar formatting methods:

That's also not quite a beginner-level technique, but may be of interest.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!