Home United States USA — software A Clarified String Formatting Cheatsheet

A Clarified String Formatting Cheatsheet

225
0
SHARE

Good to keep close by, this simplified guide to String formatting in Java covers numbers, the available flags, and handling multiple arguments.
The Java documentation of String formatting is not the easiest to read and understand if you are not familiar with String formatting or just want a quick solution. Although it is complete, it is not very user-friendly, so I thought I would try and write a clearer version. This article is as much for you as it is an aide memoir for myself.
Let’s look at some examples.
More than one placeholder can be replaced at a time. In the following example, the formatted String consists of a String and a floating point primitive.
Notice how the number is not formatted as a currency two-decimal number. Let’s look at number formatting next.
To add a number separator, include the comma character after the % placeholder.
The comma is locale-specific, so the dot (.) separator would be used in regions that use that character to group numbers.
Let’s have quick look at other number formatting options.
Padding a number with zeros is done with the 0 flag and by specifying the width. In the code below, the width is 10.
Note that the number of zeros in not 10, but the width of the number is 10 — with the remaining space after the number filled with zeros to make the number 10 digits long.
The number can be displayed justified to the left and with a given width.
Note that the number of spaces to the left is not 10, but the width of the number is 10 — with the remaining space after the number filled with the space character to make the number characters long.
There are two formatting options for displaying Octal and Hexadecimal numbers: with a leading 0 or 0x or without any leading characters.
Note the capital X in the last example. The case of the X determines the case of the X in the output number, i.e. a lowercase x results in a lowercase X in the output number.
To round-up what I have talked about so far, I have prepared a table summarizing the flags. This is not an exhaustive list, though. For that, you must consult the Java documentation.
,
Use locale-specific grouping separators (i.e., the comma in 123,456)
Only with numbers. d or f.
(
Enclose negative numbers in parentheses
Only with numbers. d or f.
The format specifier for general, character, and numeric types have the following syntax:
The values within square brackets, [], are optional. The only required elements of a format string are the percentage character % and a conversion character.
To round-up the conversion characters I have talked about I have constructed a summary table. This is not an exhaustive list, for you must consult the Java documentation.
Strings can be formatted in very much the same way as for numbers and will use many of the same flags. Let’s start by looking at a String formatted with several arguments.
The formatted string can contain multiple arguments of different types. The following example has two arguments: One is a String and the other is an integer.
Notice the format of the argument. The number refers to the order of the parameters following the String. For example, %1s refers to the 1st argument and %2d refers to the second argument.
A string can be subject to the same formatting as numbers. Let’s see some quick examples:
Here are a few compound examples that combine flags, width, precision, and a conversion character.
String formatting is a complex topic and to be sure you know all the details please refer to the Java documentation .
The code source for this article is in my GitHub repository .

Continue reading...