Syntax of String format()

There are two types of string format() methods mentioned below:

public static String format(Locale loc, String form, Object... args)
public static String format(String form, Object... args)

Parameters

locale: the locale value to be applied on the format() method
format: The format of the output string.
args: args specifying the number of arguments for the format string. It may be zero or more.

Return Value

  • Formatted string. 

Exception Thrown

Java String format() Method With Examples

In Java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.

Similar Reads

Syntax of String format()

There are two types of string format() methods mentioned below:...

Example of Java String format()

Java // Java program to demonstrate // working of format() method   // Main class class GFG {     // Main driver method     public static void main(String args[])     {         // Custom input string to be formatted         String str = "GeeksforGeeks";           // Concatenation of two strings         String s             = String.format("My Company name is %s", str);           // Output is given upto 8 decimal places         String str2             = String.format("My answer is %.8f", 47.65734);           // Here answer is supposed to be %15.8f" and         // "47.65734000" there are 15 spaces         String str3 = String.format("My answer is %15.8f",                                     47.65734);           // Print and display strings         System.out.println(s);         System.out.println(str2);         System.out.println(str3);     } }...

Java Format Specifiers

...

Contact Us