En Java, Méthode format de chaîne() renvoie une chaîne formatée en utilisant le donné locale , précisé formater la chaîne , et arguments . Nous pouvons concaténer les chaînes en utilisant cette méthode et en même temps, nous pouvons formater la chaîne concaténée de sortie.
Syntaxe du format String()
Il existe deux types de format de chaîne() méthodes mentionnées ci-dessous :
public static String format (Locale loc , String form , Object... args ) public static String format (String form , Object... args )>
Paramètres
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.>
Valeur de retour
- Chaîne formatée.
Exception levée
- NullPointerException : Si le format est nul.
- IllégalFormatException : Si le format spécifié est illégal ou si les arguments sont insuffisants.
Exemple de format de chaîne Java()
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);> >}> }> |
>
>Sortir
My Company name is techcodeview.com My answer is 47.65734000 My answer is 47.65734000>
Spécificateurs de format Java
| Spécificateur de format | Type de données | Valeur de sortie ou de retour |
|---|---|---|
| %un | point flottant | Renvoie une sortie hexadécimale d'un nombre à virgule flottante |
| %b | N'importe quel type | Vrai ou faux |
| %c | personnage | Caractère Unicode |
| %d | entier | Entier décimal |
| %C'est chaîne de format Java | point flottant | un nombre décimal en notation scientifique |
| %F | point flottant | nombre décimal |
| %g | point flottant | nombre décimal, éventuellement en notation scientifique selon la précision et la valeur |
| %h | N'importe quel type | Chaîne hexadécimale de valeur de la méthode hashCode() |
| %n | Aucun | Séparateur de lignes spécifique à la plate-forme |
| %O | entier | Nombre octal |
| %s | N'importe quel type | Valeur de chaîne |
| %t | Date/Heure | %t est le préfixe pour les conversions Date/Heure. |
| %X | entier | Chaîne hexagonale |
Exemples de spécificateurs de format de chaîne 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);> >}> }> |
>
>Sortir
My Company name is: GFG, GFG and techcodeview.com>
Exemple 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);> >}> }> |
>
>Sortir
0007044>