Java String format() is an inbuilt method that is used to return formatted strings using given locale, format, and arguments. If you don’t specify the locale in the String format() method, by default, it uses the Locale.getDefault() method. Using this method, we can concatenate the strings, and at the same time, we can format the output concatenated strings.
Java String Format
The Java String format() method returns the formatted string by given locale, format, and arguments.
If you don’t specify the locale in String.format() method, it uses default locale by calling Locale.getDefault() method.
The format() method of java language is like sprintf() function in c language and printf() method of java language.
See the following syntax.
public static String format(Locale loc, String form, Object... args)
Here on the above in syntax, the loc refers to the locale value to be applied on the format() method, form refers to the format of the output string, args refers to the number of arguments for the format string. It may be zero or more.
The method returns the formatted string.
Exception: This method has two types of exception; they are:
- NullPointerException -If the string format is null.
- IllegalFormatException – If the format specified is illegal or there are insufficient arguments in the method.
See the following code.
class JavaStrFormat { public static void main(String args[]) { String s = "Appdividend"; // Concatenating the strings using format String s1 = String.format("Welcome to %s", s); System.out.println(s1); // Left Padding using format() int x = 15; String str2 = String.format("%05d", x); System.out.println(str2); // Float value using String format String str3 = String.format("%f", 16.10); System.out.println(str3); // Hexadecimal value using String format String str4 = String.format("%x", 189); System.out.println(str4); // Char value using String format String str5 = String.format("%c", 'P'); System.out.println(str5); // Octal value using String format() String str6 = String.format("%o", 189); System.out.println(str6); } }
See the output.
Format Specifiers
Here is a quick reference to all the conversion specifiers supported:
SPECIFIER | APPLIES TO | OUTPUT |
%a | floating-point (except BigDecimal) | Hex output of the floating-point number |
%b | Any type | Returns “true” if non-null returns “false” if null |
%c | character | Unicode character |
%d | integer (incl. byte, short, int, long, bigint) | Returns decimal Integer |
%e | floating point | Returns decimal number in scientific notation |
%f | floating point | Returns decimal number |
%g | floating point | Returns decimal number, possibly in scientific notation depending on the precision and value. |
%h | any type | Returns the Hex String of value from the hashCode() method. |
%n | none | Returns the Platform-specific line separator. |
%o | integer (incl. byte, short, int, long, bigint) | Returns the Octal number |
%s | any type | Returns 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) | Returns the Hex string. |
Finally, String Format In Java Tutorial is over.
Recommended Posts
Java String getBytes() Example
How To Convert String Characters To Lowercase