Java String format()

The 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.

Internal implementation
public static String format(String format, Object... args) { return new Formatter().format(format, args).toString(); }
Signature:

There are two type of string format() method:

public static String format(Locale loc, String form, Object… args) and, public static String format(String form, Object… args)
Parameters

locale : specifies the locale to be applied on the format() method.

format : format of the string.

args : arguments for the format string. It may be zero or more.

Returns

formatted string

Throws

NullPointerException : if format is null.

IllegalFormatException : if format is illegal or incompatible.

Java String format() method example
public class Test1 { public static void main(String args[]) { String name="Rama"; String sf1=String.format("name is %s",name); String sf2=String.format("value is %f",31.36743); String sf3=String.format("value is %31.36f",31.36231); System.out.println(sf1); System.out.println(sf2); System.out.println(sf3); } }


Output:
name is Rama value is 31.367430 value is 31.362310000000000000000000000000000000
Java String format() method example
class Test1 { public static void main(String args[]) { String str = "cprogramcoding"; // Concatenation of two strings String pr1 = String.format("My Company name is %s", str); // Output is given upto 8 decimal places String pr2 = String.format("My answer is %.8f", 31.31835); // between "My answer is" and "47.65734000" there are 15 spaces String pr3 = String.format("My answer is %15.8f", 36.31735); System.out.println(pr1); System.out.println(pr2); System.out.println(pr3); } }


Output:
My Company name is cprogramcoding My answer is 31.31835000 My answer is 36.31735000
Java String format() method example
class Test1 { public static void main(String args[]) { String str1 = "CPC"; String str2 = "cprogramcoding"; //%1$ represents first argument, %2$ second argument String pr1 = String.format("My Company name" + " is: %1$s, %1$s and %2$s", str1, str2); System.out.println(pr1); } }


Output:
My Company name is: CPC, CPC and cprogramcoding
Java String Format Specifiers

Here, we are providing a table of format specifiers supported by the Java String.

Format 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 (incl. 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 (incl. byte, short, int, long, bigint) Octal number
%s any type String value
%t Date/Time (incl. long, Calendar, Date and TemporalAccessor) %t is the prefix for Date/Time conversions. More formatting flags are needed after this. See Date/Time conversion below.
%x integer (incl. byte, short, int, long, bigint)

Hex string.

Java String format() Method Example
public class Test1 { public static void main(String[] args) { String str1 = String.format("%d", 311); // Integer value String str2 = String.format("%s", "Rama"); // String value String str3 = String.format("%f", 151.20); // Float value String str4 = String.format("%x", 121); // Hexadecimal value String str5 = String.format("%c", 'p'); // Char value System.out.println(str1); System.out.println(str2); System.out.println(str3); System.out.println(str4); System.out.println(str5); } }


Output:
311 Rama 151.200000 79 p
Java String format() Method Example

Apart from formatting, we can set width, padding etc. of any value. Let us see an example where we are setting width and padding for an integer value.

public class Test1 { public static void main(String[] args) { String str1 = String.format("%d", 131); String str2 = String.format("|%10d|", 136); // Specifying length of integer String str3 = String.format("|%-10d|", 111); // Left-justifying within the specified width String str4 = String.format("|% d|", 151); String str5 = String.format("|%010d|", 172); // Filling with zeroes System.out.println(str1); System.out.println(str2); System.out.println(str3); System.out.println(str4); System.out.println(str5); } }


Output:
131 | 136| |111 | | 151| |0000000172|



Instagram