GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java String format - formatting strings in Java Java String formatlast modified July 4, 2024 In this article we show how to format strings in Java. In Java, we have methods for string formatting. Another way to dynamically create strings is string building.
The %[argument_index$][flags][width][.precision]conversion The format specifiers for general, character, and numeric types have this syntax. %[argument_index$][flags][width]conversion This is the syntax for types which are used to represents dates and times.
The format specifiers begin with the The argument_index is a decimal integer indicating the position of the argument in the argument list. The flags is a set of characters that modify the output format. The set of valid flags depends on the conversion. The width is a non-negative decimal integer indicating the minimum number of characters to be written to the output. The precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion. The required conversion is a character indicating how the argument should be formatted. Java String format methodsWe use the three methods to format a simple message. Main.java
void main() {
String name = "John Doe";
String occupation = "gardener";
String txt = "%s is a %s";
String msg = txt.formatted(name, occupation);
System.out.println(msg);
System.out.format("%s is a %s\n", name, occupation);
System.out.printf("%s is a %s%n", name, occupation);
}
We build the same string three times. String name = "John Doe"; String occupation = "gardener"; String txt = "%s is a %s"; String msg = txt.formatted(name, occupation);
The System.out.format("%s is a %s\n", name, occupation);
System.out.printf("%s is a %s%n", name, occupation);
The $ java Main.java John Doe is a gardener John Doe is a gardener John Doe is a gardener Java String format specifiersNext we use two basic format specifiers. Main.java
void main() {
System.out.format("There are %d %s.%n", 5, "pencils");
System.out.printf("The rock weighs %f kilograms.%n", 5.345);
}
In this program, we format two simple sentences. System.out.format("There are %d %s.%n", 5, "pencils");
In this code line, we have three format specifiers. Each specifier starts with
the System.out.printf("The rock weighs %f kilograms.%n", 5.345);
The $ java Main.java There are 5 pencils. The rock weighs 5.345000 kilograms. Java String format argument indexIn the next example, we work with argument indexes. Main.java
import java.time.LocalDateTime;
void main() {
int x = 12;
int y = 32;
int z = 43;
LocalDateTime dt = LocalDateTime.now();
System.out.format("There are %d apples, %d oranges and "
+ "%d pears%n", x, y, z);
System.out.format("There are %2$d apples, %3$d oranges and "
+ "%1$d pears%n", x, y, z);
System.out.format("Year: %tY, Month: %<tm, Day: %<td%n", dt);
}
The example uses argument index to refer to variables included the list of arguments. System.out.format("There are %d apples, %d oranges and "
+ "%d pears%n", x, y, z);
If we do not specify the index, the variables automatically match
the specifiers. The System.out.format("There are %2$d apples, %3$d oranges and "
+ "%1$d pears%n", x, y, z);
The System.out.format("Year: %tY, Month: %<tm, Day: %<td%n", c);
The $ java Main.java There are 12 apples, 32 oranges and 43 pears There are 32 apples, 43 oranges and 12 pears Year: 2022, Month: 10, Day: 17 Java String format flag
The flag modifies the format in a specific way. There are several flags
available. For instance, the Main.java
void main() {
System.out.format("%+d%n", 553);
System.out.format("%010d%n", 553);
System.out.format("%10d%n", 553);
System.out.format("%-10d%n", 553);
System.out.format("%d%n", -553);
System.out.format("%(d%n", -553);
}
The example presents a few flags of the string format specifier. System.out.format("%010d%n", 553);
The System.out.format("%10d%n", 553);
Without the System.out.format("%-10d%n", 553);
The System.out.format("%d%n", -553);
System.out.format("%(d%n", -553);
By default, negative numbers have a minus sign. If we use the $ java Main.java
+553
0000000553
553
553
-553
(553)
Java String format widthThe width field is the minimum number of characters to be written to the output. It cannot be used together with the line separator. Main.java
void main() {
System.out.println(1);
System.out.println(16);
System.out.println(1655);
System.out.println(16567);
System.out.println(166701);
System.out.format("%10d%n", 1);
System.out.format("%10d%n", 16);
System.out.format("%10d%n", 1655);
System.out.format("%10d%n", 16567);
System.out.format("%10d%n", 166701);
}
First, we print five numbers without specifying the field width. The width of the output is equal to the number of the characters being displayed. In the second case, we have a field width of 10. Each of the 5 outputs has a minimum length of 10 characters. The numbers are right aligned. System.out.format("%10d%n", 1);
Number 10 states that the string output must have at least ten characters. $ java Main.java
1
16
1655
16567
166701
1
16
1655
16567
166701
We can see that in the second case the numbers are right aligned. Java String format precisionThe precision field has different meaning for different conversions. For general argument types, the precision is the maximum number of characters to be written to the output. Main.java
void main() {
System.out.format("%.3g%n", 0.0000006);
System.out.format("%.3f%n", 54.34263);
System.out.format("%.3s%n", "ZetCode");
}
The precision specifier is demonstrated on three different outputs. System.out.format("%.3g%n", 0.0000006);
If the System.out.format("%.3f%n", 54.34263);
For floating point values, the precision is the number of digits after the decimal separator. System.out.format("%.3s%n", "ZetCode");
For strings, it is the maximum number of printed characters. Only three characters out of seven are printed to the console. $ java Main.java 6.00e-07 54.343 Zet Java String format numbersThe next example formats numeric data. Main.java
void main() {
System.out.format("%d%n", 12263);
System.out.format("%o%n", 12263);
System.out.format("%x%n", 12263);
System.out.format("%e%n", 0.03452342263);
System.out.format("%d%%%n", 45);
}
The example demonstrates the standard formatting specifiers for numbers. System.out.format("%d%n", 12263);
The System.out.format("%o%n", 12263);
The System.out.format("%x%n", 12263);
With the System.out.format("%e%n", 0.03452342263);
Using the System.out.format("%d%%%n", 45);
The $ java Main.java 12263 27747 2fe7 3.452342e-02 45% Java String format date and timeFinally, we format date and time data. Main.java
import java.time.LocalDateTime;
void main() {
LocalDateTime ldt = LocalDateTime.now();
System.out.format("%tF%n", ldt);
System.out.format("%tD%n", ldt);
System.out.format("%tT%n", ldt);
System.out.format("%1$tA, %1$tb %1$tY%n", ldt);
System.out.format("%1$td.%1$tm.%1$tY%n", ldt);
}
The example demonstrates the standard formatting specifiers for dates. The
conversion part of the date and time format string starts with the
System.out.format("%tF%n", c);
This line prints a date in a complete ISO 8601 format, as a result of
the System.out.format("%1$td.%1$tm.%1$tY%n", c);
Using these format specifiers, we print a date in the form that is used in
Slovakia. The parts are separated by the dot character and the day precedes the
month and the month precedes the year. All three format specifiers refer to the
$ java Main.java 2024-07-04 07/04/24 13:39:10 Thursday, Jul 2024 04.07.2024 Java localized String formatWe can pass the locale to the formatting methods. Main.java
import java.time.LocalDate;
import java.util.Locale;
void main() {
double val = 12_568_120.214;
LocalDate now = LocalDate.now();
System.out.printf("%f%n", val);
System.out.printf(Locale.FRENCH, "%f%n", val);
System.out.printf("%tA%n", now);
System.out.printf(Locale.FRENCH, "%tA%n", now);
}
In the example, we print values in English and French locales. $ java Main.java 12568120.214000 12568120,214000 Monday lundi SourceJava String - language reference In this article we have formatted strings in Java. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |