Metoda Java String format () vrací formátovaný řetězec na základě předaného argumentu.
Syntaxe format()
metody String je:
String.format(String format, Object… args)
Tady,
format()
je statická metoda.format()
Metodu voláme pomocí názvu třídyString
.…
ve výše uvedeném kódu znamená, že můžete předat více než jeden objektformat()
.
format () Parametry
format()
Metoda má dva parametry.
- format - formátovací řetězec
- args - 0 nebo více argumentů
format () návratová hodnota
- vrací formátovaný řetězec
Příklad 1: Formát Java String ()
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 ) )
Ve výše uvedeném programu si všimněte kódu
result = String.format("Language: %s", language);
Tady "Language: %s"
je formátovací řetězec .
%s
ve formátu je řetězec nahrazen obsahem jazyka. %s
je specifikátor formátu.
Podobně %x
je nahrazen hexadecimální hodnotou čísla v String.format("Number: %x", number)
.
Specifikátory formátu
Zde jsou běžně používané specifikátory formátu:
Specifikátor | Popis |
---|---|
%b , %B | "true" nebo "false" na základě argumentu |
%s , %S | řetězec |
%c , %C | znak Unicode |
%d | desítkové celé číslo (používá se pouze pro celá čísla) |
%o | osmičkové celé číslo (používá se pouze pro celá čísla) |
%x , %X | hexadecimální celé číslo (používá se pouze pro celá čísla) |
%e , %E | pro vědeckou notaci (používá se pro čísla s plovoucí desetinnou čárkou) |
%f | pro desetinná čísla (používá se pro čísla s plovoucí desetinnou čárkou) |
Příklad 2: Formátování řetězců čísel
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 ) )
Výstup
n1 v osmičkové soustavě: 57 n1 v šestnáctkové soustavě: 2f n1 v šestnáctkové soustavě: 2F n1 jako řetězec: 47 n2 jako řetězec: 35,864 n3 ve vědecké notaci: 4,45343e + 07
V řetězci formátu můžete použít více než jeden specifikátor formátu.
Příklad 3: Použití více než jednoho specifikátoru formátu
// 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)); ) )
Výstup
Výsledek šestnáctkový: 2f
Zde %s
je nahrazen hodnotou textu. Podobně %o
je nahrazen hexadecimální hodnotou n1.

Příklad 4: Formátování desetinných čísel
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 ) )
Výstup
n1 = -452,533997 n2 = -345,766000 n1 = -452,53 n2 = -345,77
Poznámka: Když naformátujeme -452,534 pomocí %f
, dostaneme -452,533997 . Není to kvůli format()
metodě. Java nevrací přesnou reprezentaci čísel s plovoucí desetinnou čárkou.
Když %.2f
je použit specifikátor formátu, format()
dává dvě čísla za desetinnou čárkou.
Příklad 5: Vyplnění čísel s mezerami a 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); ) )
Příklad 6: Použití 0x a 0 před hexadecimálním a osmičkovým
// 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 ) )
Formát Java String () s národním prostředím
Metoda String format()
má také další syntaxi, pokud musíte pracovat se zadaným národním prostředím.
String.format(Locale l, String format, Object… args)
Příklad 7: Použití německého národního prostředí ve formátu ()
// 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); ) )
Výstup
Číslo: 8 652 145 Číslo v němčině: 8.652.145
Poznámka: V Německu jsou celá čísla oddělena .
místo ,
.