logo

Mètode Java String format() amb exemples

A Java, Mètode String format(). retorna una cadena formatada utilitzant el donat locals , especificat cadena de format , i arguments . Podem concatenar les cadenes mitjançant aquest mètode i, al mateix temps, podem formatar la cadena concatenada de sortida.

Sintaxi del format String()

Hi ha dos tipus de format de cadena () mètodes esmentats a continuació:



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

Paràmetres

 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.>

Valor de retorn

  • Cadena formatada.

Excepció llançada

  • NullPointerException: Si el format és nul.
  • IllegalFormatException: Si el format especificat és il·legal o no hi ha suficients arguments.

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



>

valor de cadena de

>

Sortida

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

Especificadors de format Java

Especificador de format

Tipus de dades Valor de sortida o retorn

%a

punt flotant Retorna una sortida hexadecimal de nombre de coma flotant

%b

Qualsevol tipus Cert o fals

%c

personatge Caràcter Unicode

%d

enter Enter decimal

%És

punt flotant un nombre decimal en notació científica

%f

constructors en java
punt flotant nombre decimal

%g

punt flotant nombre decimal, possiblement en notació científica segons la precisió i el valor

%h

Qualsevol tipus Cadena hexadecimal de valor del mètode hashCode().

%n

programació stdin c
Cap Separador de línia específic per a la plataforma

%O

enter Nombre octal

%s

Qualsevol tipus Valor de cadena

%t

Data i hora %t és el prefix per a les conversions de data/hora.

%x

enter Corda hexagonal

Exemples d'especificadors de format de cadena Java

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

>

>

1nf 2nf 3nf
Sortida

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

Exemple 2

Java




java char a cadena
// 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);> >}> }>

>

>

Sortida

0007044>