Java String pad with zero example shows how to left pad or right pad string with zero in Java. The example also shows how to left pad string with zero, right pad string with zero using various ways.
How to pad a string with zero in Java?
There are various ways using which you can pad a string with zero in Java as given below.
1) Using the substring method
The substring method of String class can be used to pad string with zero as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.javacodeexamples.stringexamples; public class JavaStringPadZeroExample { //required string length after padding static final int STR_LEGTH = 10; /* * String having pad characters STR_LENGTH times. * Replace 0 with any character you want to pad with. */ static final String PAD_STRING = "0000000000"; public static void main(String[] args) { String strNumber = "12345"; System.out.println( "String left pad with zero: " + leftPad(strNumber) ); System.out.println( "String right pad with zero: " + rightPad(strNumber) ); } //string left pad with zero private static String leftPad(String str){ if(str.length() < STR_LEGTH ) return PAD_STRING.substring(str.length()) + str; else return str; } //string right pad with zero private static String rightPad(String str){ if(str.length() < STR_LEGTH ) return str + PAD_STRING.substring(str.length()); else return str; } } |
Output
1 2 |
String left pad with zero: 0000012345 String right pad with zero: 1234500000 |
The above approach does not use any external library to pad string. We first stored the required string length and pad characters. For padding left with zeros, we first took a substring from the padding character string starting from index equal to the string size we want to pad, which gave us (required length – string length) pad characters.
For example, if our string is 4 characters, substring will give us 6 zeros (10 – 4). We then concatenate the padding string and original string which gave us padded string with zero.
You can change the required length and pad character string as per your requirements.
2) Using String format (Java 1.5 and above)
String class in Java 1.5 introduced a new method named format
which can be used to pad the string with zeros. The format
method can make a string of required length by adding spaces either on the left or the right side of the String. You can replace the spaces with zeros to left pad or right pad string with zeros as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javacodeexamples.stringexamples; public class JavaStringPadZeroExample { public static void main(String[] args) { String strNumber = "12345"; //left pad with zeros String strLeftPad = String .format("%10s", strNumber) .replace(" " , "0"); System.out.println("Left padded String: " + strLeftPad); //right pad with zeros String strRightPad = String .format("%-10s", strNumber) .replace(" " , "0"); System.out.println("Right padded String: " + strRightPad); } } |
Output
1 2 |
String left pad with zero: 0000012345 String right pad with zero: 1234500000 |
The above example uses 10 as the desired length and zeroes as a padding character. You may change both of them according to your needs.
Important Note: This approach does not work if the original string already contained spaces. We are replacing all the spaces with zeros, so if String had spaces in it, they would also be replaced.
3) Using StringBuffer/StringBuilder
Probably the simplest approach is to use StringBuffer or StringBuilder of the required string length and append zeros until the desired length to left pad or right pad string as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package com.javacodeexamples.stringexamples; public class JavaStringPadZeroExample { //required string length after padding static final int STR_LEGTH = 10; //char to pad, zero in this case static final char PAD_CHAR = '0'; public static void main(String[] args) { String strNumber = "12345"; System.out.println( "String left pad with zero: " + leftPad(strNumber) ); System.out.println( "String right pad with zero: " + rightPad(strNumber) ); } //string left pad with zero private static String leftPad(String str){ StringBuilder sb = new StringBuilder(STR_LEGTH); while( sb.length() < (STR_LEGTH - str.length()) ) sb.append(PAD_CHAR); //append original string sb.append(str); return sb.toString(); } //string right pad with zero private static String rightPad(String str){ StringBuilder sb = new StringBuilder(STR_LEGTH); //append original string sb.append(str); while(sb.length() < STR_LEGTH) sb.append(PAD_CHAR); return sb.toString(); } } |
Output
1 2 |
String left pad with zero: 0000000001 String right pad with zero: 1000000000 |
Note: If you are using Java 1.4 or lower version, use the StringBuffer class instead of the StringBuilder class.
4) Java String pad with zero using Apache Commons library
If you are using the Apache Commons library, you can use the leftPad
method of StringUtils class to left pad a string or the rightPad
method to right pad a string.
1 |
static String leftPad(String str, int size, char paddingChar) |
This method left pad a string with specified paddingChar till the specified size.
1 |
static String rightPad(String str, int size, char paddingChar) |
This method right pad a string with specified paddingChar till specified size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javacodeexamples.stringexamples; import org.apache.commons.lang3.StringUtils; public class JavaStringPadZeroExample { public static void main(String[] args) { String strNumber = "12345"; //left pad with zeros String strLeftPad = StringUtils.leftPad(strNumber, 10, '0'); System.out.println("Left padded String: " + strLeftPad); //right pad with zeros String strRightPad = StringUtils.rightPad(strNumber, 10, '0'); System.out.println("Right padded String: " + strRightPad); } } |
Output
1 2 |
Left padded String: 0000012345 Right padded String: 1234500000 |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.