Metoda Java String format() z przykładami
na Jawie, Metoda formatu ciągu(). zwraca sformatowany ciąg znaków, korzystając z podanych lokalny , określony ciąg formatujący , I argumenty . Za pomocą tej metody możemy łączyć ciągi znaków, a jednocześnie możemy sformatować wyjściowy połączony ciąg.
Składnia formatu ciągu()
Istnieją dwa rodzaje format ciągu() metody wymienione poniżej:
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.
Wartość zwracana
- Sformatowany ciąg.
Zgłoszono wyjątek
- Wyjątek NullPointer: Jeśli format ma wartość null.
- Wyjątek IllegalFormat: Jeśli określony format jest nieprawidłowy lub nie ma wystarczających argumentów.
Przykład formatu Java String()
Jawa
// 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);> > }> }> |
Wyjście
My Company name is techcodeview.com My answer is 47.65734000 My answer is 47.65734000
Specyfikatory formatu Java
| Specyfikator formatu | Typ danych | Wartość wyjściowa lub zwracana |
|---|---|---|
| %A | zmiennoprzecinkowy | Zwraca wynik szesnastkowy liczby zmiennoprzecinkowej |
| %B | Dowolny typ | Prawda czy fałsz |
| %C | postać | Znak Unicode |
| %D | liczba całkowita | Dziesiętna liczba całkowita |
| %To jest | zmiennoprzecinkowy | liczba dziesiętna w notacji naukowej |
| %F | zmiennoprzecinkowy | liczba dziesiętna |
| %G | zmiennoprzecinkowy | liczba dziesiętna, ewentualnie w notacji naukowej, w zależności od precyzji i wartości |
| %H | Dowolny typ | Szesnastkowy ciąg wartości z metody hashCode(). |
| %N | Nic | Separator linii specyficzny dla platformy |
| %O | liczba całkowita | Liczba ósemkowa |
| %S | Dowolny typ | Wartość ciągu |
| %T | Data/godzina | %t to przedrostek konwersji daty/godziny. |
| %X | liczba całkowita | Sześciokątny ciąg |
Przykłady specyfikatorów formatu ciągu Java
Przykład 1
Jawa
// 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);> > }> }> |
Wyjście
My Company name is: GFG, GFG and techcodeview.com
Przykład 2
Jawa
// 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);> > }> }> |
Wyjście
0007044