Skip to main content

String Formatting And Parsing

String Formatting in Java (%s, %d, %f)

Learn Java String formatting with String.format() method using %s, %d, %f placeholders. Master format specifiers for strings, integers, floats with decimal control, padding, alignment & leading zeros. Complete tutorial with examples for clean, professional Java output.

String formatting is a way to create more readable and structured strings by inserting values into a string in a flexible and controlled way. Java provides a useful method called String.format() for this purpose. It allows you to create strings with placeholders and format numbers, text, and other values neatly.

Introduction to String.format()

The String.format() method allows you to build strings that include variables (like numbers or text) in specific positions. It uses placeholders and formatting specifiers to control how the inserted values should appear.

Syntax:

String formattedString = String.format("Your message here with %s and %d", "text", 123);

In this example:

  • %s is a placeholder for a string (text).
  • %d is a placeholder for an integer (whole number).
  • The values "text" and 123 are inserted into the placeholders.

Placeholders and Formatting Specifiers

Placeholders (also called format specifiers) are symbols inside the format string that tell Java what type of value to expect and how to display it. Here are some of the most commonly used specifiers:

  • %s: For strings (text).
  • %d: For integers (whole numbers).
  • %f: For floating-point numbers (numbers with decimals).
  • %%: To insert a literal % character.

Formatting with %s and %d

String name = "Alice";
int age = 25;

String result = String.format("My name is %s, and I am %d years old.", name, age);
System.out.println(result);  // Outputs: "My name is Alice, and I am 25 years old."

Here:

  • %s is replaced by the string "Alice".
  • %d is replaced by the integer 25.

Formatting with %f for Floating-Point Numbers

double price = 12.99;
String result = String.format("The price is %.2f dollars.", price);
System.out.println(result);  // Outputs: "The price is 12.99 dollars."

In this example, %.2f tells Java to format the number with 2 decimal places. The result is "12.99". Without the .2, Java would show more decimal places by default.

Advanced Formatting Options

Java allows you to customize the appearance of values using more advanced formatting options, such as alignmentpadding, and setting decimal places.

Specifying Decimal Places

As shown earlier, you can control the number of decimal places when formatting floating-point numbers.

  • %.2f: 2 decimal places.
  • %.3f: 3 decimal places.
double pi = 3.14159;
String result = String.format("Pi to 3 decimal places: %.3f", pi);
System.out.println(result);  // Outputs: "Pi to 3 decimal places: 3.142"

Padding and Alignment

You can use formatting options to control how values are aligned (left or right) and how much space they occupy. This is useful for creating tables or nicely formatted output.

  • %-10s: Left-align a string within a space of 10 characters.
  • %10s: Right-align a string within a space of 10 characters.
String leftAligned = String.format("%-10s | %-10s", "Name", "Age");
String rightAligned = String.format("%10s | %10s", "Alice", "25");

System.out.println(leftAligned);   // Outputs: "Name       | Age       "
System.out.println(rightAligned);  // Outputs: "     Alice |        25"

In this example:

  • %-10s left-aligns the text in a space of 10 characters.
  • %10s right-aligns the text in a space of 10 characters.

Padding Numbers with Leading Zeros

You can add leading zeros to numbers by specifying the total width and using the 0 flag before the width.

int number = 7;
String result = String.format("%03d", number);
System.out.println(result);  // Outputs: "007"

Here, %03d ensures the number is at least 3 digits long, padding it with leading zeros if necessary.

Combining Multiple Format Specifiers

You can combine multiple format specifiers in a single String.format() call, mixing strings, integers, and floating-point numbers.

String name = "Bob";
int age = 30;
double salary = 50000.567;

String result = String.format("Name: %s, Age: %d, Salary: %.2f", name, age, salary);
System.out.println(result);  // Outputs: "Name: Bob, Age: 30, Salary: 50000.57"

In this case:

  • %s inserts the name "Bob".
  • %d inserts the age 30.
  • %.2f inserts the salary with 2 decimal places.

Summary

  • String.format() is used to create formatted strings with placeholders.
  • Placeholders/Format Specifiers like %s%d, and %f allow you to insert strings, integers, and floating-point numbers into a string.
  • Advanced formatting options let you control decimal places, align text, and pad numbers with spaces or zeros.

By mastering String.format(), you can create clean and professional-looking output that’s easy to read and understand!