This is how I left pad a string with zeroes:
String.format("%16s", cardTypeString.trim()).replace(' ', '0');
Is there a better way to do this? I don't like the .replace(' ', '0')
part.
You may use StringUtils from Apache Commons:
StringUtils.leftPad(cardTypeString, 16, '0');
Implement you own PadLeft
:
public static String padLeft(String value, int width, char pad) {
if (value.length() >= width)
return value;
char[] buf = new char[width];
int padLen = width - value.length();
Arrays.fill(buf, 0, padLen, pad);
value.getChars(0, value.length(), buf, padLen);
return new String(buf);
}
See IDEONE demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With