Java String format() Metoda s příklady
v Javě, Metoda string format(). vrátí naformátovaný řetězec pomocí daného místní , specifikováno formátovací řetězec , a argumenty . Pomocí této metody můžeme řetězce zřetězit a zároveň můžeme výstupní zřetězený řetězec naformátovat.
Syntaxe String format()
Existují dva typy formát řetězce () níže uvedené metody:
public static String format (Locale loc , String form , Object... args ) public static String format (String form , Object... args )
Parametry
locale: the locale value to be applied on the format() method format: The format of the output string. args: args specifying the number of arguments for the format string. It may be zero or more.
Návratová hodnota
- Formátovaný řetězec.
Vyhozena výjimka
- NullPointerException: Pokud je formát null.
- IllegalFormatException: Pokud je zadaný formát neplatný nebo chybí dostatečné argumenty.
Příklad formátu Java String()
Jáva
// Java program to demonstrate> // working of format() method> // Main class> class> GFG {> > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom input string to be formatted> > String str => 'techcodeview.com'> ;> > // Concatenation of two strings> > String s> > = String.format(> 'My Company name is %s'> , str);> > // Output is given upto 8 decimal places> > String str2> > = String.format(> 'My answer is %.8f'> ,> 47.65734> );> > // Here answer is supposed to be %15.8f' and> > // '47.65734000' there are 15 spaces> > String str3 = String.format(> 'My answer is %15.8f'> ,> > 47.65734> );> > // Print and display strings> > System.out.println(s);> > System.out.println(str2);> > System.out.println(str3);> > }> }> |
Výstup
My Company name is techcodeview.com My answer is 47.65734000 My answer is 47.65734000
Specifikátory formátu Java
| Specifikátor formátu | Datový typ | Výstupní nebo návratová hodnota |
|---|---|---|
| %A | plovoucí bod | Vrátí hexadecimální výstup čísla s plovoucí desetinnou čárkou |
| %b | Jakýkoliv typ | Pravda nebo lež |
| %C | charakter | Unicode znak |
| %d | celé číslo | Desetinné celé číslo |
| %To je | plovoucí bod | desetinné číslo ve vědeckém zápisu |
| %F | plovoucí bod | desetinné číslo |
| %G | plovoucí bod | desetinné číslo, případně ve vědeckém zápisu v závislosti na přesnosti a hodnotě |
| %h | Jakýkoliv typ | Hexadecimální řetězec hodnoty z metody hashCode(). |
| %n | Žádný | Oddělovač řádků specifický pro platformu |
| %Ó | celé číslo | Osmičkové číslo |
| %s | Jakýkoliv typ | Hodnota řetězce |
| %t | Čas schůzky | %t je předpona pro převod data/času. |
| %X | celé číslo | Hexadecimální řetězec |
Příklady specifikátorů formátu Java String
Příklad 1
Jáva
// Java program to demonstrate Concatenation of Arguments> // to the string using format() method> // Main class> class> GFG {> > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom input string to be formatted> > String str1 => 'GFG'> ;> > String str2 => 'techcodeview.com'> ;> > // %1$ represents first argument> > // %2$ second argument> > String str = String.format(> > 'My Company name'> > +> ' is: %1$s, %1$s and %2$s'> ,> > str1, str2);> > // Print and display the formatted string> > System.out.println(str);> > }> }> |
Výstup
My Company name is: GFG, GFG and techcodeview.com
Příklad 2
Jáva
// Java program to Illustrate Left Padding> // using format() method> // Main class> class> GFG {> > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom integer number> > int> num => 7044> ;> > // Output is 3 zero's('000') + '7044',> > // in total 7 digits> > String str = String.format(> '%07d'> , num);> > // Print and display the formatted string> > System.out.println(str);> > }> }> |
Výstup
0007044