Metoda Java String format() cu exemple

În Java, Metoda String format(). returnează un șir formatat folosind datele date local , precizat șir de format , și argumente . Putem concatena șirurile folosind această metodă și, în același timp, putem formata șirul concatenat de ieșire.

Sintaxa String format()

Există două tipuri de format șir () metodele mentionate mai jos:

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

Parametrii

 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. 

Valoare returnată

  • Șir formatat.

Excepție aruncată

  • NullPointerException: Dacă formatul este nul.
  • IllegalFormatException: Dacă formatul specificat este ilegal sau există argumente insuficiente.

Exemplu de format 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);> > }> }>

Ieșire

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

Specificatori de format Java

Specificator de format

Tip de date Valoarea de ieșire sau de returnare

%A

punctul de plutire Returnează o ieșire Hex a numărului în virgulă mobilă

%b

Orice tip Adevărat sau fals

%c

caracter caracter Unicode

%d

întreg Număr întreg zecimal

%Este

punctul de plutire un număr zecimal în notație științifică

%f

punctul de plutire numar decimal

%g

punctul de plutire număr zecimal, eventual în notație științifică în funcție de precizie și valoare

%h

Orice tip Hex șir de valoare din metoda hashCode().

%n

Nici unul Separator de linie specific platformei

%O

întreg Numărul octal

%s

Orice tip Valoare șir

%t

Data/Ora %t este prefixul pentru conversiile date/ora.

%X

întreg Snur hexagonal

Exemple de specificatori de format de șiruri Java

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

Ieșire

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

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

Ieșire

0007044