Image

Imageutil wrote in Imagejava_dev

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;