The Java string format()
method returns a formatted string based on the locale, format, and arguments specified. This method can concatenate strings and format the resulting concatenated string.
The format() method in Java is similar to the sprintf()
function in C and the Printf()
function in Golang.
public class Blog {
public static void main(String[] args) {
String str = "Hack The Developer";
// format string
String formatStr = String.format("Hello: %s", str);
System.out.println(formatStr);
}
}
In this article:
show
String format() Syntax
The syntax of the Java String format()
method is:
public static String format(String form, Object... args)
Parameters
- The format of the output string.
- args specifying the number of arguments for the format string. It may be zero or more.
String format() Example:
Here is the Java String format() example:
public class Blog {
public static void main(String[] args) {
String str = "Hack The Developer";
// format string
String sf1 = String.format("Hello: %s", str);
String sf2 = String.format("value is %f", 17.452342);
String sf3 = String.format("value is %32.12f", 17.452342);
System.out.println(sf1);
System.out.println(sf2);
System.out.println(sf3);
}
}
Output:
Hello: Hack The Developer
value is 32.334340
value is 32.334340000000
Format Specifiers
The following are some of the most prevalent format specifiers for the String format() function:
Format Specifier | Data Type | Description |
%b , %B | Any | “true” if non-null, “false” if null |
%s , %S | String | String value |
%c , %C | char | A Unicode character |
%d | integer (byte, short, int, long, bigint) | Decimal Integer |
%o | integer (byte, short, int, long, bigint) | Octal Integer |
%x , %X | integer (byte, short, int, long, bigint) | Hexadecimal Integer |
%e , %E | Float | A decimal number in scientific notation |
%f | Float | Decimal number |
%n | None | Platform-specific line separator |
%t | Date/Time | Date/Time value |
%h | Any | Hex String of value from hashCode() method |
Example:
String str1 = String.format("%d", 100); // Integer value
String str2 = String.format("%s", "HTD"); // String value
String str3 = String.format("%f", 100.00); // Float value
String str4 = String.format("%x", 100); // Hexadecimal value
String str5 = String.format("%c", 'd'); // Char value
Java String format() with Locale
If you need to operate with the supplied locale, the String format() method has a different syntax.
Syntax:
public static String format(Locale loc, String form, Object... args)
Example:
import java.util.Locale;
public class Blog {
public static void main(String[] args) {
int number = 20051238;
// using the current locale
String result = String.format("Number: %,d", number);
System.out.println(result);
// GERMAN locale
String germanResult = String.format(Locale.GERMAN, "Number in German: %,d", number);
// US Locale
String usResult = String.format(Locale.US, "Number in US: %,d", number);
// FRENCH locale
String frenchResult = String.format(Locale.FRENCH, "Number in French: %,d", number);
// INDIAN locale
String indianResult = String.format(Locale.forLanguageTag("en-IN"), "Number in Indian: %,d", number);
System.out.println(germanResult);
System.out.println(usResult);
System.out.println(frenchResult);
System.out.println(indianResult);
}
}
Output:
Number: 20,051,238
Number in German: 20.051.238
Number in US: 20,051,238
Number in French: 20?051?238
Number in Indian: 20,051,238
Learn about Java Virtual Machine.