TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Padding: Pad Left and Right of Strings

Use padding on strings. Pad to the left and to the right with string.Format codes.
Padding strings. A padded string has leading or trailing spaces up to a certain number of characters. But no built-in "padding" method is available on the String class.
Instead, we implement padding with format strings and the String.format method. We can specify left and right padding. We can pad strings and numbers.
First example. This program left-justifies color names and right-justifies a parallel array of ints. We do this with just one format string.

Pad, right: The code -10s adds right-padding to a string. We must specify the minus sign here.

Pad, left: The code 10d adds left padding to an integer ("s" would handle a string).

Result: The format string applies padding to two values at once. This is efficient and correct.

Java program that uses padding, String.format public class Program { public static void main(String[] args) { String[] colors = { "red", "orange", "blue", "green" }; int[] numbers = { 42, 100, 200, 90 }; // Loop over both arrays. for (int i = 0; i < colors.length; i++) { // Left-justify the color string. // ... Right-justify the number. String line = String .format("%1$-10s %2$10d", colors[i], numbers[i]); System.out.println(line); } } } Output red 42 orange 100 blue 200 green 90
Right-justify. This program shows how the String.format method can be used to right-justify a string. It uses a total width of 10 chars.

Result: The string "Ulysses" is 7 chars, so 3 space chars are added to the start to get to 10 chars.

Java program that uses padding, right-justification public class Program { public static void main(String[] args) { String title = "Ulysses"; // Pad this string with spaces on the left to 10 characters. String padded = String.format("%1$10s", title); // Display result. System.out.print("["); System.out.print(padded); System.out.println("]"); } } Output [ Ulysses]
Left-justify. Here we left-justify a string. We expand the string to 20 chars, adding spaces on the right as needed. We must specify the minus sign.
Java program that uses padding, left-justification public class Program { public static void main(String[] args) { String title = "Finnegans Wake"; // Left-justify this title to 20 characters. String padded = String.format("%1$-20s", title); // Our result. System.out.print("|"); System.out.print(padded); System.out.println("|"); } } Output |Finnegans Wake |
Custom methods. The String.format method is an effective way of aligning text with padding. But we could wrap these String.format calls in methods.

And: Custom methods (like padLeft and padRight) would be easier to call in a program.

However: With methods that only pad one value, an entire line cannot be composed in one statement. This would likely be less efficient.

A summary. With String.format we apply padding to strings and numbers. We can pad many values at once with the format string syntax. This is powerful but can be complex.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf