Java String - format() Method
The Java format() method returns a formatted string using the specified locale(or default locale), specified format string and arguments.
Syntax
public static String format(String format, Object... args) public static String format(Locale l, String format, Object... args)
Parameters
format |
Specify a format string. |
args |
Specify the arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. |
l |
Specify the locale to apply during formatting. |
Return Value
Returns a formatted string.
Exception
Throws IllegalFormatException, If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.
Example:
In the example below, format() method returns a formatted string using default locale, specified format string and arguments.
public class MyClass { public static void main(String[] args) { String str1 = String.format("1. %s", "Hello"); String str2 = String.format("2. %f", 10.565); //returns 10 fractional character filling with 0 String str3 = String.format("3. %5.10f", 10.565); //printing all formatted strings System.out.println(str1); System.out.println(str2); System.out.println(str3); } }
The output of the above code will be:
1. Hello 2. 10.565000 3. 10.5650000000
Java String format Specifier
The table below describes the format specifier supported by Java String.
Specifier | Data Type | Output |
---|---|---|
%a | floating point except BigDecimal | Returns Hex output of floating point number |
%b | Any type | "true" if non-null, "false" if null |
%c | character | Unicode character |
%d | integer including byte, short, int, long, bigint | Decimal Integer |
%e | floating point | 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 including byte, short, int, long, bigint | Octal number |
%s | Any type | String value |
%t | Date/Time including long, Calendar, Date and TemporalAccessor | %t is the prefix for Date/Time conversions. More formatting flags are needed after this. |
%x | integer including byte, short, int, long, bigint | Hex string |
Example:
In the example below, multiple arguments are used to return the formatted string.
public class MyClass { public static void main(String[] args) { String name = "John"; int age = 25; //formatting string String MyString = String.format("My name is %s and" + " I am %d years old.", name, age); //printing the formatted string System.out.println(MyString); } }
The output of the above code will be:
My name is John and I am 25 years old.
Example:
In the example below, the string is formatted using specified locale.
import java.util.*; public class MyClass { public static void main(String[] args) { String name = "Marry"; int age = 22; Locale l = new Locale("English", "US"); //formatting string using specified locale String MyString = String.format(l, "My name is %s and" + " I am %d years old.", name, age); //printing the formatted string System.out.println(MyString); } }
The output of the above code will be:
My name is Marry and I am 22 years old.
❮ Java String Methods