Java String format() method
The Java String format() method is used for formatting strings. Similar to the printf() method, the format() method comes with a range of format specifiers and sub-specifiers that give developers a comprehensive option list to choose from when displaying data. Unlike the printf() method, the format() method output can be stored in a string variable for use in other parts of your application like a GUI text field or started for use later.
The Java String format() method uses a specified locale (n object which represents a specific geographical, political, or cultural region), format string, and object arguments. The local is not required, however the format string and object argument are required.
Datatype | Parameter | Description |
---|---|---|
Locale | l | Specifies which locale to use in the formatting. If it's null, the default Locale is used |
String | format | Defines the format of the Object (below) |
Object | args | The object that will be formatted based the format (above) |
There are two general syntax options for the format() method:
- String.format(locale, String format, Object… args);
- String.format(String format, Object… args);
1. String.format(locale, String format, Object… args);
Use this option if you require formatting that has geographical, political, or cultural significance. For example, characters that represent a decimal differ around the world. In the US, UK and Ireland, a (decimal) point or dot is used; 123.99. In some European countries, a comma is used to represent a decimal: 123,99.
We can use Java’s Locale to format an output to account for geographical differences.
String FR = String.format(Locale.FRANCE, "version is %f ",123.99); String US = String.format(Locale.US, "version is %f ",123.99); System.out.println("The FR " + FR); System.out.println("The US " + US);
Notice the difference in decimal character in the output below. The French decimal character is a comma whereas the US uses a point:
run: The FR version is 123,990000 The US version is 123.990000
We can see that the println() method uses a string concatenated with a variable which is a typical use of that method.
System.out.println(“The FR ” + FR);
The format() method is used to format a string that, in this case, we are assigning to a string variable.
String FR = String.format(Locale.FRANCE, “version is %f “,123.99);
This format() method has three parts:
- locale
- format
- argument
The first part is the locale and we have discussed that earlier. The second part contains a format specifier that will dictate how the third part will be formatted.
String.format(Locale.US, “version is %f “,123.99);
How does this work exactly:
- String.format has an argument contained within round brackets as follows:
- The locale will change anything that varies by region (for example the way most countries in Europe use a comma for decimals but the US, UK and Ireland use a full stop or period).
- A comma separates locale from the next part of the argument.
- The % character is a placeholder and acts like a flag that Java will pay attention to.
- The letter after the % character (in this case f for float) will determine how and what Java will place at that point in the outputted string.
- A comma separates the string from the next part of the argument
- Finally, the data (in this case 123.99) that will replace the % symbol and its character ends the argument.
This is a simple example, you can have more complex strings with multiple data pieces.
2. String.format(String format, Object… args);
**INCOMPLETE 240328**