Java String - format() Method
The java.lang.String.format() method returns a formatted string using the specified locale, specified format string and arguments.
Syntax
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 specified locale, specified format string and arguments.
import java.lang.*; import java.util.*; public class MyClass { public static void main(String[] args) { //creating a locale Locale l = new Locale("English", "US"); String str1 = String.format(l, "1. %s", "Hello"); String str2 = String.format(l, "2. %f", 10.565); //returns 10 fractional character filling with 0 String str3 = String.format(l, "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.
import java.lang.*; 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.lang - String