Op Java, Tekenreeksformat() methode retourneert een opgemaakte tekenreeks met behulp van de opgegeven waarde lokaal , gespecificeerd opmaakreeks , En argumenten . We kunnen de tekenreeksen aaneenschakelen met behulp van deze methode en tegelijkertijd kunnen we de samengevoegde uitvoerreeks opmaken.
Syntaxis van String-formaat()
Er zijn twee soorten tekenreeksformaat() hieronder genoemde methoden:
public static String format (Locale loc , String form , Object... args ) public static String format (String form , Object... args )>
Parameters
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.>
Winstwaarde
- Opgemaakte tekenreeks.
Uitzondering gegooid
- NullPointerException: Als het formaat nul is.
- IllegaleFormaatUitzondering: Als het opgegeven formaat illegaal is of als er onvoldoende argumenten zijn.
Voorbeeld van Java String-formaat()
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);> >}> }> |
>
>
nietUitvoer
My Company name is techcodeview.com My answer is 47.65734000 My answer is 47.65734000>
Java-formaatspecificaties
| Formaatspecificatie | Data type | Uitvoer- of retourwaarde |
|---|---|---|
| %A | drijvende punt | Retourneert een Hex-uitvoer van een drijvende-kommagetal |
| %B | Elk type | Waar of niet waar |
| %C | karakter | Unicode-teken |
| %D | geheel getal | Decimaal geheel getal |
| %Het is | drijvende punt | een decimaal getal in wetenschappelijke notatie |
| %F | drijvende punt | decimaal getal |
| %G | drijvende punt | decimaal getal, mogelijk in wetenschappelijke notatie, afhankelijk van de nauwkeurigheid en waarde |
| %H string naar json-object | Elk type | Hex waardereeks uit de hashCode()-methode |
| %N | Geen | Platformspecifieke lijnscheider |
| %O | geheel getal | Octaal getal |
| %S git-opdrachten voor push | Elk type | Tekenreekswaarde |
| %T | Datum Tijd | %t is het voorvoegsel voor datum-/tijdconversies. |
| %X | geheel getal | Hex-reeks |
Voorbeelden van Java String Format Specificifiers
voorbeeld 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);> >}> }> |
>
>Uitvoer
My Company name is: GFG, GFG and techcodeview.com>
Voorbeeld 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);> >}> }> |
Hoe verander je de string in int?
>
>Uitvoer
0007044>