HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java – Right pad a string with spaces or zeros

Java – Right pad a string with spaces or zeros

Java programs to add right padding to a string in such a way that total string length should be a fixed predefined number.

For example, if we have a string of length 10, and we want to increase it’s length to 15 – by adding right padding then use the example given this post.

1. How right padding is added

When you add right padding, you essentially add a character repeatedly to the end of string – until string length reaches to defined length.

For example –

howtodoinjava.com		//no padding
howtodoinjava.com    		//right padding of 4 spaces
howtodoinjava.com....		//right padding of 4 dots
howtodoinjava.com0000		//right padding of 4 zeros

Read More: Java remove trailing whitespaces from String

2. Java right padding with spaces

To add right padding, most useful and easy way is to use StringUtils.rightPad() method.

2.1. Maven dependency

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

2.2. Method Syntax (overloaded)

/**
 * @param str  		- the String to pad out, may be null
 * @param size 		- the size to pad to i.e. Total length of result string
 * @param padChar  	- the character or string to pad with
 * 
 * @return right padded String or original String if no padding is necessary or if null String input
 */

public static String rightPad(final String str, final int size) {...}
public static String rightPad(final String str, final int size, String padStr) {...}
public static String rightPad(final String str, final int size, final char padChar) { ... }

2.3. Right padding example

//Added # character to verify padded blank spaces

System.out.println( StringUtils.rightPad("howtodoinjava", 20, " ") + "#");
System.out.println( StringUtils.rightPad("howtodoinjava", 30, " ") + "#");
System.out.println( StringUtils.rightPad("howtodoinjava", 15, " ") + "#");

Program output:

howtodoinjava       #
howtodoinjava                 #
howtodoinjava  #

3. Java right pad a string with zeros

Java program to use StringUtils.rightPad() method to right pad a string with zeros, by adding trailing zeros to string.

System.out.println( StringUtils.rightPad("0123456789", 10, "0") );
System.out.println( StringUtils.rightPad("0123", 10, "0") );
System.out.println( StringUtils.rightPad("012345", 10, "0") );

Program output:

0123456789
0123000000
0123450000

4. Summary

In above examples, we learned to right pad string with spaces to fixed length. We also saw to right pad a number with zeros.

Use this right padding to format strings to fixed length – to display in UI.

Happy Learning !!

Reference:

StringUtils Java Doc

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Dharmedra

    March 22, 2020

    which package should be imported for using this?

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces