Site icon DataFlair

Java String format() Method with Examples

java string format()

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

The format() method in Java is an essential tool for creating formatted strings by combining a format string with specific arguments. It provides a mechanism to format these arguments within a string according to a specified format.

The primary purpose of the format() method is to offer a versatile way to concatenate strings with other variables or values while maintaining a structured and organized format. Instead of manually concatenating strings, you can employ format specifiers within the format string, which will be seamlessly substituted with the provided arguments.

Syntax of Java String format()

The syntax of the two format() methods is:

// With locale
public static String format(Locale loc, String format, Object... args)

// Without locale 
public static String format(String format, Object... args)

The parameters are:

This method returns the formatted string.

It throws:

Java Format Specifiers

Format Specifier Data Type Output or Return value
%a floating point Returns a Hex output of floating point number
%b Any type True or False
%c character Unicode character
%d integer Decimal Integer
%e floating point A decimal number in scientific notation
%f floating point Decimal number
%g floating point Decimal number, possibly in scientific notation, depending on the precision and value
%h Any type Hex String of value from hashCode() method
%n None Platform-specific line separator
%o integer Octal number
%s Any type String value
%t Date/Time %t is the prefix for Date/Time conversions.
%x integer Hex string

Examples

Example 1: Formatting Personal Information

public class IndianPersonalInfo {
    public static void main(String[] args) {
        String name = "Rajesh";
        int age = 28;
        String city = "Mumbai";

        String formattedString = String.format("Name: %s, Age: %d, City: %s", name, age, city);
        System.out.println(formattedString);
    }
}

Output:
Name: Rajesh, Age: 28, City: Mumbai

Example 2: Formatting Currency

public class IndianCurrency {
    public static void main(String[] args) {
        double price = 999.50;

        String formattedPrice = String.format("Price: ₹%.2f", price);
        System.out.println(formattedPrice);
    }
}

Output:
Price: ₹999.5

Conclusion

In conclusion, the format() method in Java is a robust resource for shaping strings according to your specific requirements. With the aid of format specifiers, it grants you the flexibility to format arguments of diverse data types, simplifying string concatenation and enhancing readability.

An array of predefined format specifiers and flags is available, enabling you to fine-tune the formatting, including aspects like padding, precision, sign, radix, and more. Mastery of the format() method and its various specifier types will undoubtedly empower you to craft well-structured strings, making data output a more efficient and effective process. We recommend practising with different specifier types to become proficient in utilizing the format() method to its fullest potential.

Exit mobile version