- 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
Left pad a string in Java
To left pad a string, use the String.format and set the spaces.
String.format("|%20s|", "demotext")
If you add 30 above, it will display the first string after 30 spaces from the beginning.
String.format("|%30s|", "demotext")
Example
public class Demo { public static void main(String []args) { System.out.print(String.format("|%20s|", "demotext")); System.out.println("Left padded!"); } }
Output
| demotext|Left padded
- Related Articles
- Left pad a String in Java with zeros
- Left pad a String with a specified String in Java
- Left pad a String with a specified character in Java
- Left pad a String with spaces (' ') in Java
- Right pad a string in Java
- Left pad an integer in Java with zeros
- MySQL number-string formatting to pad zeros on the left of a string with numbers after a slash
- In MySQL, how can we pad a string with another string?
- How to update multiple rows and left pad values in MySQL?
- Pad a string using random numbers to a fixed length using JavaScript
- Split the left part of a string by a separator string in MySQL?
- Shift left in a BigInteger in Java
- How to pad a number with leading zeros in JavaScript?
- Left justify output in Java
- How to get a string left padded with zeros in Python?

Advertisements