Last active
December 16, 2015 19:19
-
-
Save kchenthilrajan/5484384 to your computer and use it in GitHub Desktop.
Utility function to prepend,append and trim zeros to a string
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
public static String prependZero(String givenStr,int expectedLength){ | |
if(StringUtils.isNotEmpty(givenStr)&&givenStr.length()>=expectedLength){ | |
return givenStr; | |
} | |
return prependZero("0"+givenStr,expectedLength); | |
} | |
public static String appendZero(String givenStr,int expectedLength){ | |
if(StringUtils.isNotEmpty(givenStr)&&givenStr.length()>=expectedLength){ | |
return givenStr; | |
} | |
return appendZero(givenStr+"0",expectedLength); | |
} | |
public static String trimLeadingZeros(String givenString){ | |
return givenString.replaceFirst("^0+(?!$)", ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment