Top.Mail.Ru
February 23rd, 2004 - Java developers — LiveJournal
? ?

Java developers

February 23rd, 2004

03:42 pm - util - Left padding string

Say you're given a String s of length <= n. How would you do the following? Create a String of length n that has s on the right side and the left filled with copies of some char c.

I've come across this problem a couple of times in making HTML hex color codes. My solution is the following. I'm hoping someone else has something much more elegant to offer. Thanks!

Color color;
//...
String[] padding =
    {"000000", "00000", "0000", "000", "00", "0", ""};
String hexString =
    Integer.toHexString(color.getRGB() & 0xffffff);
String hexCode = "#" + padding[hexString.length()] + hexString;

05:41 pm - talldean

In the last topic, someone pointed out that a String is a constant; after it's initialized, to change it, the computer allocates another String. StringBuffer is a true variable string.

Question: shouldn't this be something pretty easy to have a compiler optimize? Essentially, my question is, "does javac do any optimizations? does gjc?" and "does jikes optimize this?" Anyone know?
Powered by LiveJournal.com