A Giava, Metodo formato stringa() restituisce una stringa formattata utilizzando il dato locale , specificato stringa di formato , E argomenti . Possiamo concatenare le stringhe utilizzando questo metodo e allo stesso tempo possiamo formattare la stringa concatenata di output.
Sintassi di String format()
Ne esistono due tipi formato stringa() metodi indicati di seguito:
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.>
Valore di ritorno
- Stringa formattata.
Eccezione lanciata
- NullPointerException: Se il formato è nullo.
- IllegalFormatException: Se il formato specificato è illegale o non sono presenti argomenti sufficienti.
Esempio di formato stringa Java()
Giava
// 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);> >}> }> |
>
>Produzione
My Company name is techcodeview.com My answer is 47.65734000 My answer is 47.65734000>
Specificatori di formato Java
| Identificatore di formato | Tipo di dati | Valore di output o di ritorno |
|---|---|---|
| %UN | virgola mobile | Restituisce un output esadecimale di un numero in virgola mobile |
| %B | Qualsiasi tipo | Vero o falso |
| %C | carattere | Carattere Unicode |
| %D | numero intero | Intero decimale |
| %È | virgola mobile | un numero decimale in notazione scientifica |
| %F | virgola mobile | numero decimale |
| %G | virgola mobile | numero decimale, eventualmente in notazione scientifica a seconda della precisione e del valore |
| %H conversione del tipo e casting in Java | Qualsiasi tipo | Hex Stringa di valore dal metodo hashCode() |
| %N | Nessuno | Separatore di linea specifico per la piattaforma |
| %O | numero intero | Numero ottale |
| %S | Qualsiasi tipo | Valore stringa |
| %T | Appuntamento | %t è il prefisso per le conversioni di data/ora. |
| %X | numero intero | Stringa esadecimale |
Esempi di specificatori di formato stringa Java
Esempio 1
Giava
// 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);> >}> }> |
>
>Produzione
My Company name is: GFG, GFG and techcodeview.com>
Esempio 2
Giava
domande per l'intervista in lingua 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);> >}> }> |
>
>Produzione
0007044>