Java String format() Metóda s príkladmi

v Jave Metóda string format(). vráti naformátovaný reťazec pomocou daného miestne , špecifikované formátovací reťazec , a argumenty . Pomocou tejto metódy môžeme reťazce zreťaziť a zároveň môžeme naformátovať výstupný zreťazený reťazec.

Syntax String format()

Existujú dva typy formát reťazca () metódy uvedené nižšie:

public static String format (Locale loc , String form , Object... args ) public static String format (String form , Object... args ) 

Parametre

 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ý reťazec.

Vhodená výnimka

  • NullPointerException: Ak je formát null.
  • IllegalFormatException: Ak je zadaný formát nezákonný alebo neexistujú dostatočné argumenty.

Príklad formátu Java String()

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);> > }> }>

Výkon

My Company name is techcodeview.com My answer is 47.65734000 My answer is 47.65734000 

Špecifikátory formátu Java

Špecifikátor formátu

Dátový typ Výstupná alebo návratová hodnota

%a

s pohyblivou rádovou čiarkou Vráti hexadecimálny výstup čísla s pohyblivou rádovou čiarkou

%b

Akýkoľvek typ Pravda alebo lož

%c

charakter Unicode znak

%d

celé číslo Desatinné celé číslo

%To je

s pohyblivou rádovou čiarkou desatinné číslo vo vedeckom zápise

%f

s pohyblivou rádovou čiarkou desatinné číslo

%g

s pohyblivou rádovou čiarkou desatinné číslo, prípadne vo vedeckej notácii v závislosti od presnosti a hodnoty

%h

Akýkoľvek typ Hexadecimálny reťazec hodnoty z metódy hashCode().

%n

žiadne Oddeľovač riadkov špecifický pre platformu

%O

celé číslo Osmičkové číslo

%s

Akýkoľvek typ Hodnota reťazca

%t

Dátum Čas %t je predpona pre konverzie dátumu a času.

%X

celé číslo Šesťhranný reťazec

Príklady špecifikátorov formátu reťazcov Java

Príklad 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);> > }> }>

Výkon

My Company name is: GFG, GFG and techcodeview.com 

Príklad 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);> > }> }>

Výkon

0007044