
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
Advertisements