Java String format() Method With Examples

04 Oct, 2021
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.
Syntax: There is two types of string format() method
public static String format(Locale loc, String form, Object... args)
public static String format(String form, Object... args)
Parameter: 
The locale value to be applied on the format() method
The format of the output string.
args specifying the number of arguments for the format string. It may be zero or more.
Return Type: Formatted string. 
Exception Thrown: 
NullPointerException: If the format is null.
IllegalFormatException: If the format specified is illegal or there are insufficient arguments.
Example 1:
java
// Java program to demonstrate// working of format() method  // Main classclass 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);    }}
My Company name is GeeksforGeeks
My answer is 47.65734000
My answer is     47.65734000
Example 2:
java
// Java program to demonstrate Concatenation of Arguments// to the string using format() method  // Main classclass GFG {      // Main driver method    public static void main(String args[])    {        // Custom input string to be formatted        String str1 = "GFG";        String str2 = "GeeksforGeeks";          // %1$ represents first argument        // %2$ second argument        String str = String.format(            "My Company name"                + " is: %1$s, %1$s and %2$s",            str1, str2);          // Print and display the formatted string        System.out.println(str);    }}
My Company name is: GFG, GFG and GeeksforGeeks
 
Example 3:
java
// Java program to Illustrate Left Padding// using format() method  // Main classclass GFG {      // Main driver method    public static void main(String args[])    {        // Custom integer number        int num = 7044;          // Output is 3 zero's("000") + "7044",        // in total 7 digits        String str = String.format("%07d", num);          // Print and display the formatted string        System.out.println(str);    }}
0007044
 
solankimayank
Java-Functions
Java-Strings
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java