Friday, February 10, 2012

Format string with leading zeros to have a fixed length

For an example lets think that you need to format a string so that it always has the length of 8 characters. Following method always returns a String which has the length of 8.

private String formatString(String myString) {
   String baseString = "00000000";//8 zeros
   String tempString = baseString + myString;
   return tempString.substring(myString.length());
}

No comments:

Post a Comment