- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Right pad a string in Java
To right pad a string, use the String.format and set the spaces.
String.format("%1$-" + 20 + "s", "demotext"));
If you add 30 above, it will display the next string after 30 spaces from the beginning.
String.format("%1$-" + 30 + "s", "demotext")
The following is an example.
Example
public class Demo { public static void main(String []args){ System.out.print(String.format("%1$-" + 20 + "s", "demotext")); System.out.println("Right padded!"); } }
Output
demotext Right padded!
- Related Articles
- Left pad a string in Java
- Left pad a String with a specified String in Java
- Left pad a String in Java with zeros
- Left pad a String with a specified character in Java
- Left pad a String with spaces (' ') in Java
- In MySQL, how can we pad a string with another string?
- Left pad an integer in Java with zeros
- Pad a string using random numbers to a fixed length using JavaScript
- MySQL number-string formatting to pad zeros on the left of a string with numbers after a slash
- Shift right in a BigInteger in Java
- How to pad a number with leading zeros in JavaScript?
- Insert a String into another String in Java
- Tokenizing a String in Java
- Split a string in Java
- How to get a space-padded string with the original string right-justified in Python?

Advertisements