Skip to content

Instantly share code, notes, and snippets.

@kchenthilrajan
Last active December 16, 2015 19:19
Show Gist options
  • Save kchenthilrajan/5484384 to your computer and use it in GitHub Desktop.
Save kchenthilrajan/5484384 to your computer and use it in GitHub Desktop.
Utility function to prepend,append and trim zeros to a string
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