

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 with a specified String in Java
The following is our string.
String str = "Jack";
Now take a StringBuilder object.
StringBuilder strBuilder = new StringBuilder();
Perform left padding and extend the string length to 20. The string that will be padded comes on the left.
while (strBuilder.length() + str.length() < 20) { strBuilder.append("demo"); }
The following is an example wherein we have left padded a string with another string “demo”
Example
public class Demo { public static void main(String[] args) { String str = "Jack"; StringBuilder strBuilder = new StringBuilder(); // left padding with a string while (strBuilder.length() + str.length() < 20) { strBuilder.append("demo"); } // append strBuilder.append(str); String res = strBuilder.toString(); System.out.println(res); } }
Output
demodemodemodemoJack
- Related Questions & Answers
- Left pad a String with a specified character in Java
- Left pad a string in Java
- Left pad a String in Java with zeros
- Left pad a String with spaces (' ') in Java
- Right pad a string in Java
- 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?
- Left pad an integer in Java with zeros
- How to check if a string ends with a specified Suffix string in Golang?
- How to check if a string starts with a specified Prefix string in Golang?
- Split the left part of a string by a separator string in MySQL?
- Pad a string using random numbers to a fixed length using JavaScript
- How to return a new string with a specified number of copies of an existing string with JavaScript?
- Limiting string up to a specified length in JavaScript
- Insert a String into another String in Java
Advertisements