Die Java String format () -Methode gibt eine formatierte Zeichenfolge zurück, die auf dem übergebenen Argument basiert.
Die Syntax der String- format()
Methode lautet:
String.format(String format, Object… args)
Hier,
format()
ist eine statische Methode. Wir rufen dieformat()
Methode mit dem Klassennamen aufString
.…
im obigen Code bedeutet, dass Sie mehr als ein Objekt an übergeben könnenformat()
.
format () Parameter
Die format()
Methode akzeptiert zwei Parameter.
- format - eine Formatzeichenfolge
- args - 0 oder mehr Argumente
format () Rückgabewert
- Gibt eine formatierte Zeichenfolge zurück
Beispiel 1: Java String Format ()
class Main ( public static void main(String() args) ( String language = "Java"; int number = 30; String result; // format object as a string result = String.format("Language: %s", language); System.out.println(result); // Language: Java // format number as a hexadecimal number result = String.format("Hexadecimal Number: %x", number); // 1e System.out.println(result); // Hexadecimal Number: 1e ) )
Beachten Sie im obigen Programm den Code
result = String.format("Language: %s", language);
Hier "Language: %s"
ist eine Formatzeichenfolge .
%s
im Format wird die Zeichenfolge durch den Inhalt der Sprache ersetzt. %s
ist ein Formatbezeichner.
Ebenso %x
wird durch den Hexadezimalwert der Zahl in ersetzt String.format("Number: %x", number)
.
Formatbezeichner
Hier sind die häufig verwendeten Formatspezifizierer:
Spezifizierer | Beschreibung |
---|---|
%b , %B | "true" oder "false" basierend auf dem Argument |
%s , %S | ein Faden |
%c , %C | ein Unicode-Zeichen |
%d | eine Dezimalzahl (wird nur für Ganzzahlen verwendet) |
%o | eine oktale Ganzzahl (wird nur für Ganzzahlen verwendet) |
%x , %X | eine hexadezimale Ganzzahl (wird nur für Ganzzahlen verwendet) |
%e , %E | für wissenschaftliche Notation (wird für Gleitkommazahlen verwendet) |
%f | für Dezimalzahlen (wird für Gleitkommazahlen verwendet) |
Beispiel 2: Zeichenfolgenformatierung von Zahlen
class Main ( public static void main(String() args) ( int n1 = 47; float n2 = 35.864f; double n3 = 44534345.76d; // format as an octal number System.out.println(String.format("n1 in octal: %o", n1)); // 57 // format as hexadecimal numbers System.out.println(String.format("n1 in hexadecimal: %x", n1)); // 2f System.out.println(String.format("n1 in hexadecimal: %X", n1)); // 2F // format as strings System.out.println(String.format("n1 as string: %s", n1)); // 47 System.out.println(String.format("n2 as string: %s", n2)); // 35.864 // format in scientific notation System.out.println(String.format("n3 in scientific notation: %g", n3)); // 4.45343e+07 ) )
Ausgabe
n1 in Oktal: 57 n1 in Hexadezimal: 2f n1 in Hexadezimal: 2F n1 als Zeichenfolge: 47 n2 als Zeichenfolge: 35.864 n3 in wissenschaftlicher Notation: 4.45343e + 07
Sie können mehr als einen Formatbezeichner in der Formatzeichenfolge verwenden.
Beispiel 3: Verwenden von mehr als einem Formatbezeichner
// using more than one format specifiers // in a format string class Main ( public static void main(String() args) ( int n1 = 47; String text = "Result"; System.out.println(String.format("%shexadecimal: %x", text, n1)); ) )
Ausgabe
Ergebnis hexadezimal: 2f
Hier %s
wird durch den Wert von Text ersetzt. Ebenso %o
wird durch den Hexadezimalwert von n1 ersetzt.

Beispiel 4: Formatieren von Dezimalzahlen
class Main ( public static void main(String() args) ( float n1 = -452.534f; double n2 = -345.766d; // format floating-point as it is System.out.println(String.format("n1 = %f", n1)); // -452.533997 System.out.println(String.format("n2 = %f", n2)); // -345.766000 // show up to two decimal places System.out.println(String.format("n1 = %.2f", n1)); // -452.53 System.out.println(String.format("n2 = %.2f", n2)); // -345.77 ) )
Ausgabe
n1 = -452,533997 n2 = -345,766000 n1 = -452,53 n2 = -345,77
Hinweis: Wenn wir -452.534 mit formatieren%f
, erhalten wir -452.533997 . Es liegt nicht an der format()
Methode. Java gibt nicht die genaue Darstellung von Gleitkommazahlen zurück.
Wenn der %.2f
Formatbezeichner verwendet wird, werden format()
zwei Zahlen nach dem Dezimalpunkt angegeben.
Beispiel 5: Auffüllen von Zahlen mit Leerzeichen und 0
// using more than one format specifiers // in a format string class Main ( public static void main(String() args) ( int n1 = 46, n2 = -46; String result; // padding number with spaces // the length of the string will be 5 result = String.format("|%5d|", n1); // | 46| System.out.println(result); // padding number with numbers 0 // the length of the string will be 5 result = String.format("|%05d|", n1); // |00046| System.out.println(result); // using signs before numbers result = String.format("%+d", n1); // +46 System.out.println(result); result = String.format("%+d", n2); // -46 System.out.println(result); // enclose negative number within parenthesis // and removing the sign result = String.format("%(d", n2); // (46) System.out.println(result); ) )
Beispiel 6: Verwenden von 0x und 0 vor Hexadezimal und Oktal
// using 0x before hexadecimal // using 0 before octal class Main ( public static void main(String() args) ( int n = 46; System.out.println(String.format("%#o", n)); // 056 System.out.println(String.format("%#x", n)); // 0x2e ) )
Java String Format () mit Gebietsschema
Die String- format()
Methode hat auch eine andere Syntax, wenn Sie mit dem angegebenen Gebietsschema arbeiten müssen.
String.format(Locale l, String format, Object… args)
Beispiel 7: Verwenden des DEUTSCHEN Gebietsschemas im Format ()
// to use Locale import java.util.Locale; class Main ( public static void main(String() args) ( int number = 8652145; String result; // using the current locale result = String.format("Number: %,d", number); System.out.println(result); // using the GERMAN locale as the first argument result = String.format(Locale.GERMAN, "Number in German: %,d", number); System.out.println(result); ) )
Ausgabe
Nummer: 8.652.145 Nummer in Deutsch: 8.652.145
Hinweis: In Deutschland werden Ganzzahlen durch .
statt getrennt ,
.