Formato Java String ()

Il metodo Java String format () restituisce una stringa formattata in base all'argomento passato.

La sintassi del format()metodo String è:

 String.format(String format, Object… args)

Qui,

  • format()è un metodo statico. Chiamiamo il format()metodo usando il nome della classe String.
  • nel codice sopra significa che puoi passare più di un oggetto a format().

format () Parametri

Il format()metodo accetta due parametri.

  • format - una stringa di formato
  • args - 0 o più argomenti

format () Valore restituito

  • restituisce una stringa formattata

Esempio 1: formato Java String ()

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

Nel programma precedente, notare il codice

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

Ecco "Language: %s"una stringa di formato .

%snella stringa di formato viene sostituito con il contenuto della lingua. %sè un identificatore di formato.

Allo stesso modo, %xviene sostituito con il valore esadecimale di number in String.format("Number: %x", number).

Identificatori di formato

Ecco gli identificatori di formato comunemente usati:

Specifier Descrizione
%b, %B "true"o in "false"base all'argomento
%s, %S una stringa
%c, %C un carattere Unicode
%d un numero intero decimale (utilizzato solo per i numeri interi)
%o un numero intero ottale (usato solo per i numeri interi)
%x, %X un numero intero esadecimale (utilizzato solo per i numeri interi)
%e, %E per la notazione scientifica (usato per i numeri in virgola mobile)
%f per i numeri decimali (usato per i numeri in virgola mobile)

Esempio 2: formattazione di stringhe di numeri

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

Produzione

 n1 in ottale: 57 n1 in esadecimale: 2f n1 in esadecimale: 2F n1 come stringa: 47 n2 come stringa: 35.864 n3 in notazione scientifica: 4.45343e + 07

È possibile utilizzare più di un identificatore di formato nella stringa di formato.

Esempio 3: utilizzo di più di un identificatore di formato

 // 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)); ) )

Produzione

 Risultato esadecimale: 2f

Qui, %sviene sostituito con il valore del testo. Allo stesso modo, %oviene sostituito con il valore esadecimale di n1.

Utilizzo del formato Java String ()

Esempio 4: formattazione di numeri decimali

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

Produzione

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

Nota: quando formatta -452.534 utilizzando %f, otteniamo -452.533997 . Non è a causa del format()metodo. Java non restituisce la rappresentazione esatta dei numeri in virgola mobile.

Quando %.2fviene utilizzato l' format()identificatore di formato, fornisce due numeri dopo il punto decimale.

Esempio 5: riempimento di numeri con spazi e 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); ) )

Esempio 6: utilizzo di 0x e 0 prima di esadecimale e ottale

 // 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 ) )

Formato Java String () con Locale

Il format()metodo String ha anche un'altra sintassi se devi lavorare con la locale specificata.

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

Esempio 7: utilizzo delle impostazioni internazionali TEDESCO in 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); ) )

Produzione

 Numero: 8.652.145 Numero in tedesco: 8.652.145

Nota: in Germania, gli interi sono separati da .anziché da ,.

Articoli interessanti...