Metoda Java String format() s primeri
V Javi, Metoda formata niza(). vrne oblikovani niz z uporabo danega lokalni , določeno formatni niz , in argumenti . S to metodo lahko povežemo nize in hkrati oblikujemo izhodni povezani niz.
Sintaksa formata niza()
Obstajata dve vrsti oblika niza () spodaj navedene metode:
public static String format (Locale loc , String form , Object... args ) public static String format (String form , Object... args )
Parametri
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.
Povratna vrednost
- Oblikovan niz.
Vržena izjema
- NullPointerException: Če je oblika ničelna.
- IllegalFormatException: Če je navedena oblika nezakonita ali če ni dovolj argumentov.
Primer Java String format()
Java
// 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);> > }> }> |
Izhod
My Company name is techcodeview.com My answer is 47.65734000 My answer is 47.65734000
Specifikatorji formata Java
| Specifikator formata | Vrsta podatkov | Izhodna ali vrnjena vrednost |
|---|---|---|
| %a | plavajočo vejico | Vrne šestnajstiški izhod števila s plavajočo vejico |
| %b | Katere koli vrste | Pravilno ali napačno |
| %c | značaj | Znak Unicode |
| %d | celo število | Decimalno celo število |
| %Je | plavajočo vejico | decimalno število v znanstvenem zapisu |
| %f | plavajočo vejico | decimalno število |
| %g | plavajočo vejico | decimalno število, po možnosti v znanstvenem zapisu, odvisno od natančnosti in vrednosti |
| %h | Katere koli vrste | Hex niz vrednosti iz metode hashCode(). |
| %n | Noben | Ločilo vrstic, specifično za platformo |
| %O | celo število | Osmiško število |
| %s | Katere koli vrste | Vrednost niza |
| %t | Datum čas | %t je predpona za pretvorbe datum/čas. |
| %x | celo število | Hex niz |
Primeri specifikatorjev formatov nizov Java
Primer 1
Java
// 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);> > }> }> |
Izhod
My Company name is: GFG, GFG and techcodeview.com
Primer 2
Java
// 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);> > }> }> |
Izhod
0007044