Created
February 5, 2017 16:07
-
-
Save DirkHoffmann/836fd06c846a4cdfbf0b54a9ecb2d1da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return a string padded with leading zeroes for a given total length. | |
* There are other solutions around in stackoverflow etc., in particular | |
* org.apache.commons.lang.StringUtils.leftPad(), but this one is a concise | |
* and cheap solution for the particular case of 0-padding, as a reasonable | |
* feature for fixed-length <strong>binary</strong> is missing in the String | |
* and Integer classes up to now. | |
* | |
* @param s | |
* Input string (in particular binary form of number) to be padded. | |
* @param len | |
* Total length wanted for the padded String (input + padding). | |
* @return | |
* The padded String. | |
*/ | |
static public String leftPadZeroes(String s, int len){ | |
return String.format( | |
String.format("%%0%dd", Math.max(0, len-s.length())), 0) | |
+ s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment