Padding a String with custom value in Java

Adewale Adeleye
Analytics Vidhya
Published in
3 min readOct 27, 2020

--

Java String — Edureka

In this short article, we will be exploring different methods of padding a string in Java. We will create methods that pad with default values (empty spaces), and methods that allow us to pad a String with a custom character. The String class in Java doesn’t have a padding method out of the box, so we will be creating different methods as well as use different libraries to pad a string in Java.

Padding with default value: We will create an overloaded method to pad with a default character or a custom character. The first pad method will contain an initial text and the desired length while setting the custom pad character of the second pad method to a space character.

Default pad method:

public static String pad(String text, int len){
return pad(text,len,' ');
}

Padding with a custom character: This method contains an additional character parameter that allows us to specify what character we want to pad our string with.

public static String pad(String text, int len, char value){
StringBuilder sb = new StringBuilder();
if(text.length()<len){
sb.append(text);
for(int i=text.length();i<len;i++){
sb.append(value);
}
return sb.toString();
}
return text;
}

If you notice from the above code snippets, we pad the new set of characters to the left of the initial string. Let’s create a similar method that pad from left to right.

Padding from left: The following code snippet is similar to the default padding method, but it pads the default value from the left.

public static String padLeft(String text, int len) {
return padLeft(text,len,' ');
}

Padding from left with custom character: This pads a custom set of characters from left.

public static String padLeft(String text, int len, char value){
StringBuilder sb = new StringBuilder();
if(text.length()<len){
for(int i=text.length();i<len;i++){
sb.append(value);
}
sb.append(text);
return sb.toString();
}
return text;
}

Padding using String libraries

Apache Commons Lang: This library has a couple of Java utility classes. One of the classes is the StringUtils class that has a plethora of methods to manipulate and format strings. Before you can use the library, you will need to add the dependency in your pom.xml file.

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>

According to apache-commons documentation, we can left-pad as our example above with space or with a different character. Here are sample methods found in the documentation.

leftPad(String str, int size) to pad a String with spaces.

StringUtils.leftPad(“bat”, 3) = “bat”
StringUtils.leftPad(“bat”, 5) = “ bat”

leftPad(String str, int size, char padChar) to pad a String with a specified character.

StringUtils.leftPad("bat", 3, 'z')  = "bat"
StringUtils.leftPad("bat", 5, 'z') = "zzbat"

leftPad(String str, int size, String padStr) to pad a String with a Specified String.

StringUtils.leftPad("bat", 5, "yz")  = "yzbat"
StringUtils.leftPad("bat", 8, "yz") = "yzyzybat"

rightPad(String str, int size) to right pad a String with spaces.

StringUtils.rightPad(“bat”, 3) = “bat”
StringUtils.rightPad(“bat”, 5) = “bat ”

rightPad(String str, int size, char padChar) to right pad a String with a specified character.

StringUtils.rightPad("bat", 3, 'z')  = "bat"
StringUtils.rightPad("bat", 5, 'z') = "batzz"

rightPad(String str, int size, String padStr) to right pad a String with a Specified String.

StringUtils.rightPad("bat", 5, "yz")  = "batyz"
StringUtils.rightPad("bat", 8, "yz") = "batyzyzy"

I believe that with this article, we’ve been able to cover different ways of padding a String, by implementing different methods ourselves and by using one of the most popular libraries in Java to pad a String.

--

--