Java String formatted() Method

The String.formatted() method in Java is used to format a string using specified arguments. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. formatted Method Syntax
  3. Examples
    • Basic String Formatting
    • Formatting with Multiple Arguments
    • Using Format Specifiers
  4. Conclusion

Introduction

The String.formatted() method is a member of the String class in Java, introduced in Java 15. It allows you to format a string using the same format specifiers as the String.format() method, providing a convenient way to create formatted strings.

formatted Method Syntax

The syntax for the formatted method is as follows:

public String formatted(Object... args)
  • args: The arguments to be used in the format string.

Examples

Basic String Formatting

The formatted method can be used to format a string with a single argument.

Example

public class FormattedExample {
    public static void main(String[] args) {
        String template = "Hello, %s!";

        String formattedString = template.formatted("World");

        System.out.println(formattedString);
    }
}

Output:

Hello, World!

Formatting with Multiple Arguments

The formatted method can be used to format a string with multiple arguments.

Example

public class FormattedExample {
    public static void main(String[] args) {
        String template = "Name: %s, Age: %d, City: %s";

        String formattedString = template.formatted("Alice", 30, "New York");

        System.out.println(formattedString);
    }
}

Output:

Name: Alice, Age: 30, City: New York

Using Format Specifiers

The formatted method supports various format specifiers for formatting numbers, dates, and other types of data.

Example

public class FormattedExample {
    public static void main(String[] args) {
        String template = "Integer: %d, Floating-point: %.2f, Hex: %x";

        String formattedString = template.formatted(42, 3.14159, 255);

        System.out.println(formattedString);
    }
}

Output:

Integer: 42, Floating-point: 3.14, Hex: ff

Formatting a Date

The formatted method can also be used to format dates.

Example

import java.time.LocalDate;

public class FormattedExample {
    public static void main(String[] args) {
        String template = "Today's date is %tD";

        String formattedString = template.formatted(LocalDate.now());

        System.out.println(formattedString);
    }
}

Output:

Today's date is 05/31/24

Conclusion

The String.formatted() method in Java provides a convenient way to format strings using specified arguments. By understanding how to use this method, you can efficiently create formatted strings in your Java applications. Whether you are performing basic string formatting, formatting with multiple arguments, using format specifiers, or formatting dates, the formatted method offers a powerful and flexible solution for these tasks.

Comments