Format Java String ()

Metoda Java String format () zwraca sformatowany ciąg na podstawie przekazanego argumentu.

Składnia format()metody String jest następująca:

 String.format(String format, Object… args)

Tutaj,

  • format()jest metodą statyczną. Nazywamy tę format()metodę, używając nazwy klasy String.
  • w powyższym kodzie oznacza, że ​​możesz przekazać więcej niż jeden obiekt format().

format () Parametry

format()Sposób dwa parametry.

  • format - ciąg formatu
  • args - 0 lub więcej argumentów

format () Wartość zwracana

  • zwraca sformatowany ciąg

Przykład 1: Java String format ()

 class Main ( public static void main(String() args) ( String language = "Java"; int number = 30; String result; // format object as a string result = String.format("Language: %s", language); System.out.println(result); // Language: Java // format number as a hexadecimal number result = String.format("Hexadecimal Number: %x", number); // 1e System.out.println(result); // Hexadecimal Number: 1e ) )

W powyższym programie zwróć uwagę na kod

 result = String.format("Language: %s", language);

Tutaj "Language: %s"jest ciąg formatu .

%sw formacie ciąg znaków jest zastępowany zawartością języka. %sjest specyfikatorem formatu.

Podobnie %xjest zastępowany szesnastkową wartością liczby w String.format("Number: %x", number).

Specyfikatory formatu

Oto często używane specyfikatory formatu:

Specyficzny Opis
%b, %B "true"lub "false"na podstawie argumentu
%s, %S ciąg
%c, %C znak Unicode
%d dziesiętna liczba całkowita (używana tylko dla liczb całkowitych)
%o ósemkowa liczba całkowita (używana tylko dla liczb całkowitych)
%x, %X liczba szesnastkowa (używana tylko dla liczb całkowitych)
%e, %E dla notacji naukowej (używane dla liczb zmiennoprzecinkowych)
%f dla liczb dziesiętnych (używane dla liczb zmiennoprzecinkowych)

Przykład 2: Ciągowe formatowanie liczb

 class Main ( public static void main(String() args) ( int n1 = 47; float n2 = 35.864f; double n3 = 44534345.76d; // format as an octal number System.out.println(String.format("n1 in octal: %o", n1)); // 57 // format as hexadecimal numbers System.out.println(String.format("n1 in hexadecimal: %x", n1)); // 2f System.out.println(String.format("n1 in hexadecimal: %X", n1)); // 2F // format as strings System.out.println(String.format("n1 as string: %s", n1)); // 47 System.out.println(String.format("n2 as string: %s", n2)); // 35.864 // format in scientific notation System.out.println(String.format("n3 in scientific notation: %g", n3)); // 4.45343e+07 ) )

Wynik

 n1 ósemkowo: 57 n1 szesnastkowo: 2f n1 szesnastkowo: 2F n1 jako ciąg: 47 n2 jako ciąg: 35,864 n3 w notacji naukowej: 4.45343e + 07

W ciągu formatu można użyć więcej niż jednego specyfikatora formatu.

Przykład 3: użycie więcej niż jednego specyfikatora formatu

 // using more than one format specifiers // in a format string class Main ( public static void main(String() args) ( int n1 = 47; String text = "Result"; System.out.println(String.format("%shexadecimal: %x", text, n1)); ) )

Wynik

 Wynik szesnastkowy: 2f

Tutaj %szostaje zastąpiony wartością tekstu. Podobnie %ojest zastępowane wartością szesnastkową n1.

Działanie formatu Java String ()

Przykład 4: Formatowanie liczb dziesiętnych

 class Main ( public static void main(String() args) ( float n1 = -452.534f; double n2 = -345.766d; // format floating-point as it is System.out.println(String.format("n1 = %f", n1)); // -452.533997 System.out.println(String.format("n2 = %f", n2)); // -345.766000 // show up to two decimal places System.out.println(String.format("n1 = %.2f", n1)); // -452.53 System.out.println(String.format("n2 = %.2f", n2)); // -345.77 ) )

Wynik

 n1 = -452,533997 n2 = -345,766000 n1 = -452,53 n2 = -345,77

Uwaga: kiedy formatujemy -452,534 używając %f, otrzymujemy -452,533997 . To nie z powodu format()metody. Java nie zwraca dokładnej reprezentacji liczb zmiennoprzecinkowych.

Gdy %.2fużywany jest specyfikator formatu, format()daje dwie liczby po przecinku.

Przykład 5: Wypełnienie liczb spacjami i 0

 // using more than one format specifiers // in a format string class Main ( public static void main(String() args) ( int n1 = 46, n2 = -46; String result; // padding number with spaces // the length of the string will be 5 result = String.format("|%5d|", n1); // | 46| System.out.println(result); // padding number with numbers 0 // the length of the string will be 5 result = String.format("|%05d|", n1); // |00046| System.out.println(result); // using signs before numbers result = String.format("%+d", n1); // +46 System.out.println(result); result = String.format("%+d", n2); // -46 System.out.println(result); // enclose negative number within parenthesis // and removing the sign result = String.format("%(d", n2); // (46) System.out.println(result); ) )

Przykład 6: Użycie 0x i 0 przed szesnastkowym i ósemkowym

 // using 0x before hexadecimal // using 0 before octal class Main ( public static void main(String() args) ( int n = 46; System.out.println(String.format("%#o", n)); // 056 System.out.println(String.format("%#x", n)); // 0x2e ) )

Format Java String () z ustawieniami narodowymi

Metoda String format()ma również inną składnię, jeśli musisz pracować z określonymi ustawieniami narodowymi.

 String.format(Locale l, String format, Object… args)

Przykład 7: Używanie NIEMIECKICH ustawień regionalnych w format ()

 // to use Locale import java.util.Locale; class Main ( public static void main(String() args) ( int number = 8652145; String result; // using the current locale result = String.format("Number: %,d", number); System.out.println(result); // using the GERMAN locale as the first argument result = String.format(Locale.GERMAN, "Number in German: %,d", number); System.out.println(result); ) )

Wynik

 Liczba: 8652145 Liczba w języku niemieckim: 8.652.145

Uwaga: w Niemczech liczby całkowite są oddzielane .znakiem zamiast ,.

Interesujące artykuły...