String Format
Methods:
String.format(String formatString, Object... args): Static method on theStringclass. Creates a new formatted string.printf(String formatString, Object... args): Static method on theSystemclass. Prints the formatted string to the console. Bothformat()andprintf()use the same format specifiers and arguments. Specifying what to format:%s: Inserts a string.%c: Inserts a single character.%d: Inserts a decimal integer.%f: Inserts a floating-point number.%o: Inserts an octal integer.%x: Inserts a hexadecimal integer (lowercase).%X: Inserts a hexadecimal integer (uppercase).%%: Inserts a literal percent sign (%).
Styling your formatted string:
Flags:
-: Left-justify+: Add sign for positive numbers0: Pad with zeros#: Add prefix (0 for octal, 0x for hexadecimal),: Group thousands separator
Width: Specify minimum field width (e.g.,
%10dfor 10-digit integer).Precision:
For integers: minimum number of digits (e.g.,
%.2dfor two decimal places).For floats/doubles: maximum number of decimal places (e.g.,
%.4ffor four decimal places).
Last updated