String.Format Metodo

Definizione

Converte il valore degli oggetti in stringhe in base ai formati specificati e li inserisce in un'altra stringa.

String.Format Per una rapida panoramica, vedere Introduzione al metodo String.Format.

Overload

Nome Descrizione
Format(IFormatProvider, String, Object, Object, Object)

Sostituisce gli elementi di formato in una stringa con la rappresentazione di stringa di tre oggetti specificati. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

Format(IFormatProvider, String, Object, Object)

Sostituisce gli elementi di formato in una stringa con la rappresentazione di stringa di due oggetti specificati. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

Format(String, Object, Object)

Sostituisce gli elementi di formato in una stringa con la rappresentazione di stringa di due oggetti specificati.

Format(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

Format(IFormatProvider, CompositeFormat, Object[])

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

Format(String, Object, Object, Object)

Sostituisce gli elementi di formato in una stringa con la rappresentazione di stringa di tre oggetti specificati.

Format(IFormatProvider, String, Object[])

Sostituisce gli elementi di formato in una stringa con le rappresentazioni di stringa degli oggetti corrispondenti in una matrice specificata. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

Format(IFormatProvider, String, Object)

Sostituisce l'elemento di formato o gli elementi in una stringa specificata con la rappresentazione di stringa dell'oggetto corrispondente. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

Format(String, ReadOnlySpan<Object>)

Sostituisce l'elemento di formato in una stringa specificata con la rappresentazione di stringa di un oggetto corrispondente in un intervallo specificato.

Format(String, Object[])

Sostituisce l'elemento di formato in una stringa specificata con la rappresentazione di stringa di un oggetto corrispondente in una matrice specificata.

Format(String, Object)

Sostituisce uno o più elementi di formato in una stringa con la rappresentazione di stringa di un oggetto specificato.

Format(IFormatProvider, String, ReadOnlySpan<Object>)

Sostituisce gli elementi di formato in una stringa con le rappresentazioni di stringa degli oggetti corrispondenti in un intervallo specificato. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

Format<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

Format<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

Format<TArg0>(IFormatProvider, CompositeFormat, TArg0)

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

Esempio

Numerosi esempi che chiamano il metodo Format sono diffusi in tutto l'articolo. È anche possibile scaricare un set completo di String.Format esempi, inclusi un progetto .NET Core per C#.

Di seguito sono riportati alcuni esempi inclusi nell'articolo:

Creare una stringa di formato

Inserire una stringa
Elemento di formato
Formattare gli elementi con lo stesso indice

Controllare l'output formattato

Formattazione dei controlli
Spaziatura dei controlli
Allineamento dei controlli
Controllare il numero di cifre integrali
Controllare il numero di cifre dopo il separatore decimale
Includere parentesi graffe letterali nella stringa del risultato

Rendere le stringhe di formato sensibili alle impostazioni culturali

Rendere le stringhe di formato sensibili alla cultura

Personalizzare l'operazione di formattazione

Operazione di formattazione personalizzata
Fornitore di intercettazioni e formattatore di numeri romani

Introduzione al metodo String.Format

Usare String.Format se è necessario inserire il valore di un oggetto, di una variabile o di un'espressione in un'altra stringa. Ad esempio, è possibile inserire il valore di un Decimal valore in una stringa per visualizzarlo all'utente come una singola stringa:

Decimal pricePerOunce = 17.36m;
String s = String.Format("The current price is {0} per ounce.",
                         pricePerOunce);
Console.WriteLine(s);
// Result: The current price is 17.36 per ounce.
let pricePerOunce = 17.36m
String.Format("The current price is {0} per ounce.", pricePerOunce)
|> printfn "%s"
// Result: The current price is 17.36 per ounce.
Dim pricePerOunce As Decimal = 17.36D
Dim s As String = String.Format("The current price is {0} per ounce.",
                              pricePerOunce)
' Result: The current price is 17.36 per ounce.

È anche possibile controllare la formattazione del valore:

Decimal pricePerOunce = 17.36m;
String s = String.Format("The current price is {0:C2} per ounce.",
                         pricePerOunce);
Console.WriteLine(s);
// Result if current culture is en-US:
//      The current price is $17.36 per ounce.
let pricePerOunce = 17.36m
String.Format("The current price is {0:C2} per ounce.", pricePerOunce)
|> printfn "%s"
// Result if current culture is en-US:
//      The current price is $17.36 per ounce.
Dim pricePerOunce As Decimal = 17.36D
Dim s As String = String.Format("The current price is {0:C2} per ounce.",
                              pricePerOunce)
' Result if current culture is en-US:
'      The current price is $17.36 per ounce.

Oltre alla formattazione, è anche possibile controllare l'allineamento e la spaziatura.

Inserire una stringa

String.Format inizia con una stringa di formato, seguita da uno o più oggetti o espressioni che verranno convertiti in stringhe e inseriti in una posizione specificata nella stringa di formato. Per esempio:

decimal temp = 20.4m;
string s = String.Format("The temperature is {0}°C.", temp);
Console.WriteLine(s);
// Displays 'The temperature is 20.4°C.'
let temp = 20.4m
String.Format("The temperature is {0}°C.", temp)
|> printfn "%s"
// Displays 'The temperature is 20.4°C.'
Dim temp As Decimal = 20.4D
Dim s As String = String.Format("The temperature is {0}°C.", temp)
Console.WriteLine(s)
' Displays 'The temperature is 20.4°C.'

La stringa di formato {0} è un elemento di formato. 0 è l'indice dell'oggetto il cui valore stringa verrà inserito in tale posizione. (Gli indici iniziano da 0.) Se l'oggetto da inserire non è una stringa, viene chiamato il ToString metodo per convertirlo in uno prima di inserirlo nella stringa di risultato.

Ecco un altro esempio che usa due elementi di formato e due oggetti nell'elenco di oggetti:

string s = String.Format("At {0}, the temperature is {1}°C.",
                         DateTime.Now, 20.4);
Console.WriteLine(s);
// Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'
String.Format("At {0}, the temperature is {1}°C.", DateTime.Now, 20.4)
|> printfn "%s"
// Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'
Dim s As String = String.Format("At {0}, the temperature is {1}°C.",
                              Date.Now, 20.4)
' Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'

È possibile disporre di tutti gli elementi di formato e di tutti gli oggetti nell'elenco di oggetti desiderati, purché l'indice di ogni elemento di formato abbia un oggetto corrispondente nell'elenco di oggetti. Inoltre, non è necessario preoccuparsi di quale overload si chiama; il compilatore selezionerà quello appropriato per voi.

Formattazione dei controlli

È possibile seguire l'indice in un elemento di formato con una stringa di formato per controllare la formattazione di un oggetto. Ad esempio, {0:d} applica la stringa di formato "d" al primo oggetto nell'elenco di oggetti. Di seguito è riportato un esempio con un singolo oggetto e due elementi di formato:

string s = String.Format("It is now {0:d} at {0:t}", DateTime.Now);
Console.WriteLine(s);
// Output similar to: 'It is now 4/10/2015 at 10:04 AM'
String.Format("It is now {0:d} at {0:t}", DateTime.Now)
|> printfn "%s"
// Output similar to: 'It is now 4/10/2015 at 10:04 AM'
Dim s As String = String.Format("It is now {0:d} at {0:t}",
                              Date.Now)
' Output similar to: 'It is now 4/10/2015 at 10:04 AM'

Numerosi tipi supportano stringhe di formato, inclusi tutti i tipi numerici (stringhe di formato standard e personalizzato ), tutte le date e le ore (stringhe di formato standard e personalizzato ) e gli intervalli di tempo (stringhe di formato standard e personalizzato ), tutti i tipi di enumerazione tipi di enumerazione e GUID. È anche possibile aggiungere il supporto per le stringhe di formato ai propri tipi.

Spaziatura dei controlli

È possibile definire la larghezza della stringa inserita nella stringa di risultato usando la sintassi , {0,12}che inserisce una stringa di 12 caratteri. In questo caso, la rappresentazione di stringa del primo oggetto è allineata a destra nel campo a 12 caratteri. Se la rappresentazione di stringa del primo oggetto è lunga più di 12 caratteri, tuttavia, la larghezza del campo preferito viene ignorata e l'intera stringa viene inserita nella stringa di risultato.

Nell'esempio seguente viene definito un campo a 6 caratteri per contenere la stringa "Year" e alcune stringhe di anno, nonché un campo di 15 caratteri per contenere la stringa "Population" e alcuni dati di popolamento. Si noti che i caratteri sono allineati a destra nel campo.

int[] years = { 2013, 2014, 2015 };
int[] population = { 1025632, 1105967, 1148203 };
var sb = new System.Text.StringBuilder();
sb.Append(String.Format("{0,6} {1,15}\n\n", "Year", "Population"));
for (int index = 0; index < years.Length; index++)
    sb.Append(String.Format("{0,6} {1,15:N0}\n", years[index], population[index]));

Console.WriteLine(sb);

// Result:
//      Year      Population
//
//      2013       1,025,632
//      2014       1,105,967
//      2015       1,148,203
open System
open System.Text
 
let years = [| 2013; 2014; 2015 |]
let population = [| 1025632; 1105967; 1148203 |]
let sb = StringBuilder()
sb.Append(String.Format("{0,6} {1,15}\n\n", "Year", "Population")) |> ignore
for i = 0 to years.Length - 1 do
   sb.Append(String.Format("{0,6} {1,15:N0}\n", years[i], population[i])) |> ignore

printfn $"{sb}"

// Result:
//      Year      Population
//
//      2013       1,025,632
//      2014       1,105,967
//      2015       1,148,203
Dim years() As Integer = {2013, 2014, 2015}
Dim population() As Integer = {1025632, 1105967, 1148203}
Dim sb As New StringBuilder()
sb.Append(String.Format("{0,6} {1,15}{2}{2}",
                       "Year", "Population", vbCrLf))
For index As Integer = 0 To years.Length - 1
    sb.AppendFormat("{0,6} {1,15:N0}{2}",
                  years(index), population(index), vbCrLf)
Next
' Result:
'      Year      Population
'
'      2013       1,025,632
'      2014       1,105,967
'      2015       1,148,203

Allineamento dei controlli

Per impostazione predefinita, le stringhe sono allineate a destra all'interno del relativo campo se si specifica una larghezza del campo. Per allineare le stringhe a sinistra in un campo, anteporre la larghezza del campo con un segno negativo, ad esempio {0,-12} per definire un campo allineato a sinistra di 12 caratteri.

L'esempio seguente è simile a quello precedente, ad eccezione del fatto che allinea a sinistra sia le etichette che i dati.

int[] years = { 2013, 2014, 2015 };
int[] population = { 1025632, 1105967, 1148203 };
String s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population");
for (int index = 0; index < years.Length; index++)
    s += String.Format("{0,-10} {1,-10:N0}\n",
                       years[index], population[index]);
Console.WriteLine($"\n{s}");
// Result:
//    Year       Population
//
//    2013       1,025,632
//    2014       1,105,967
//    2015       1,148,203
let years = [| 2013; 2014; 2015 |]
let population = [| 1025632; 1105967; 1148203 |]
let mutable s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population")
for i = 0 to years.Length - 1 do
    s <- s + String.Format("{0,-10} {1,-10:N0}\n", years[i], population[i])
printfn $"\n{s}"
// Result:
//    Year       Population
//
//    2013       1,025,632
//    2014       1,105,967
//    2015       1,148,203
Dim years() As Integer = {2013, 2014, 2015}
Dim population() As Integer = {1025632, 1105967, 1148203}
Dim s As String = String.Format("{0,-10} {1,-10}{2}{2}",
                               "Year", "Population", vbCrLf)
For index As Integer = 0 To years.Length - 1
    s += String.Format("{0,-10} {1,-10:N0}{2}",
                     years(index), population(index), vbCrLf)
Next
' Result:
'    Year       Population
'
'    2013       1,025,632
'    2014       1,105,967
'    2015       1,148,203

String.Format usa la funzionalità di formattazione composita. Per altre informazioni, vedere Formattazione composita.

Quale metodo chiamare?

Objective Metodo da chiamare
Formattare uno o più oggetti usando le convenzioni della cultura attuale. Ad eccezione degli overload che includono un provider parametro, gli overload rimanenti Format includono un String parametro seguito da uno o più parametri oggetto. Per questo motivo, non è necessario determinare quale Format overload si intende chiamare. Il compilatore del linguaggio seleziona l'overload appropriato tra quelli che non hanno un parametro provider, basandosi sul tuo elenco di argomenti. Ad esempio, se l'elenco di argomenti ha cinque argomenti, il compilatore chiama il Format(String, Object[]) metodo .
Formattare uno o più oggetti utilizzando le convenzioni di una cultura specifica. Ogni Format overload che inizia con un provider parametro è seguito da un String parametro e da uno o più parametri oggetto. Per questo motivo, non è necessario stabilire quale specifico overload Format si intende chiamare. Il compilatore del linguaggio seleziona l'overload appropriato tra gli overload che hanno un parametro provider, in base all'elenco degli argomenti. Ad esempio, se l'elenco di argomenti ha cinque argomenti, il compilatore chiama il Format(IFormatProvider, String, Object[]) metodo .
Eseguire un'operazione di formattazione personalizzata con un'implementazione ICustomFormatter o un'implementazione IFormattable . Uno dei quattro overload con parametro provider. Il compilatore seleziona il sovraccarico appropriato tra quelli con parametro provider, in base all'elenco di argomenti.

Il metodo Format in breve

Ogni overload del metodo Format usa la funzionalità di formattazione composita per includere segnaposti indicizzati in base zero, chiamati elementi di formato, in una stringa di formato composito. In fase di esecuzione, ogni elemento di formato viene sostituito con la rappresentazione di stringa dell'argomento corrispondente in un elenco di parametri. Se il valore dell'argomento è null, l'elemento di formato viene sostituito con String.Empty. Ad esempio, la chiamata seguente al Format(String, Object, Object, Object) metodo include una stringa di formato con tre elementi di formato, {0}, {1}e {2}e un elenco di argomenti con tre elementi.

DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0);
string city = "Chicago";
int temp = -16;
string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
                              dat, city, temp);
Console.WriteLine(output);
// The example displays output like the following:
//    At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
open System

let dat = DateTime(2012, 1, 17, 9, 30, 0) 
let city = "Chicago"
let temp = -16
String.Format("At {0} in {1}, the temperature was {2} degrees.", dat, city, temp)
|> printfn "%s"
// The example displays output like the following:
//    At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
Dim dat As Date = #1/17/2012 9:30AM#
Dim city As String = "Chicago"
Dim temp As Integer = -16
Dim output As String = String.Format("At {0} in {1}, the temperature was {2} degrees.",
                                   dat, city, temp)
Console.WriteLine(output)
' The example displays the following output:
'    At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.

Elemento di formato

Un elemento di formato ha questa sintassi:

{index[,width][:formatString]}

Le parentesi quadre indicano elementi facoltativi. Sono necessarie le parentesi graffe di apertura e chiusura. Per includere una parentesi graffa di apertura o di chiusura letterale nella stringa di formato, consultare la sezione Gestione delle parentesi graffe nell'articolo Formattazione composita.

Ad esempio, un elemento di formato per formattare un valore di valuta potrebbe essere simile al seguente:

var value = String.Format("{0,-10:C}", 126347.89m);
Console.WriteLine(value);
open System

String.Format("{0,-10:C}", 126347.89m)         
|> printfn "%s"
String.Format("{0,-10:C}", 126347.89D)

Un elemento di formato include gli elementi seguenti:

index
Indice in base zero dell'argomento di cui la rappresentazione in stringa deve essere inclusa in questa posizione nella stringa. Se questo argomento è null, verrà inclusa una stringa vuota in questa posizione nella stringa.

width
Optional. Intero con segno che indica la lunghezza totale del campo in cui viene inserito l'argomento e se è allineato a destra (un numero intero positivo) o allineato a sinistra (intero negativo). Se si omette width, la rappresentazione di stringa dell'argomento corrispondente viene inserita in un campo senza spazi iniziali o finali.

Se il valore di width è minore della lunghezza dell'argomento da inserire, width viene ignorato e la lunghezza della rappresentazione di stringa dell'argomento viene utilizzata come larghezza del campo.

formatString
Optional. Stringa che specifica il formato della stringa di risultato dell'argomento corrispondente. Se si omette formatString, viene chiamato il metodo senza ToString parametri dell'argomento corrispondente per produrre la relativa rappresentazione di stringa. Se si specifica formatString, l'argomento a cui fa riferimento l'elemento di formato deve implementare l'interfaccia IFormattable . I tipi che supportano le stringhe di formato includono:

Tuttavia, qualsiasi tipo personalizzato può implementare IFormattable o estendere l'implementazione di un tipo esistente IFormattable.

Nell'esempio seguente vengono utilizzati gli argomenti width e formatString per produrre un output formattato.

// Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
Tuple<string, DateTime, int, DateTime, int>[] cities =
    { Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277,
                   new DateTime(1950, 1, 1), 1970358),
      Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995,
                   new DateTime(1950, 1, 1), 7891957),
      Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808,
                   new DateTime(1950, 1, 1), 3620962),
      Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452,
                   new DateTime(1950, 1, 1), 1849568) };

// Display header
var header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
                              "City", "Year", "Population", "Change (%)");
Console.WriteLine(header);
foreach (var city in cities) {
   var output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
                          city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
                          (city.Item5 - city.Item3)/ (double)city.Item3);
   Console.WriteLine(output);
}
// The example displays the following output:
//    City            Year  Population    Year  Population    Change (%)
//
//    Los Angeles     1940   1,504,277    1950   1,970,358        31.0 %
//    New York        1940   7,454,995    1950   7,891,957         5.9 %
//    Chicago         1940   3,396,808    1950   3,620,962         6.6 %
//    Detroit         1940   1,623,452    1950   1,849,568        13.9 %
// Create a list of 5-tuples with population data for three U.S. cities, 1940-1950.
let cities = 
    [ "Los Angeles", DateTime(1940, 1, 1), 1504277, DateTime(1950, 1, 1), 1970358
      "New York", DateTime(1940, 1, 1), 7454995, DateTime(1950, 1, 1), 7891957
      "Chicago", DateTime(1940, 1, 1), 3396808, DateTime(1950, 1, 1), 3620962
      "Detroit", DateTime(1940, 1, 1), 1623452, DateTime(1950, 1, 1), 1849568 ]

// Display header
String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n", "City", "Year", "Population", "Change (%)")
|> printfn "%s"

for name, year1, pop1, year2, pop2 in cities do
    String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
                  name, year1, pop1, year2, pop2,
                  double (pop2 - pop1) / double pop1)
    |> printfn "%s"
// The example displays the following output:
//    City            Year  Population    Year  Population    Change (%)
//  
//    Los Angeles     1940   1,504,277    1950   1,970,358        31.0 %
//    New York        1940   7,454,995    1950   7,891,957         5.9 %
//    Chicago         1940   3,396,808    1950   3,620,962         6.6 %
//    Detroit         1940   1,623,452    1950   1,849,568        13.9 %
Module Example4
    Public Sub Main()
        ' Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
        Dim cities() =
          {Tuple.Create("Los Angeles", #1/1/1940#, 1504277, #1/1/1950#, 1970358),
            Tuple.Create("New York", #1/1/1940#, 7454995, #1/1/1950#, 7891957),
            Tuple.Create("Chicago", #1/1/1940#, 3396808, #1/1/1950#, 3620962),
            Tuple.Create("Detroit", #1/1/1940#, 1623452, #1/1/1950#, 1849568)}

        ' Display header
        Dim header As String = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}",
                                           "City", "Year", "Population", "Change (%)")
        Console.WriteLine(header)
        Console.WriteLine()
        For Each city In cities
            Dim output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
                                city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
                                (city.Item5 - city.Item3) / city.Item3)
            Console.WriteLine(output)
        Next
    End Sub
End Module
' The example displays the following output:
'    City            Year  Population    Year  Population    Change (%)
'
'    Los Angeles     1940   1,504,277    1950   1,970,358        31.0 %
'    New York        1940   7,454,995    1950   7,891,957         5.9 %
'    Chicago         1940   3,396,808    1950   3,620,962         6.6 %
'    Detroit         1940   1,623,452    1950   1,849,568        13.9 %

Modalità di formattazione degli argomenti

Gli elementi di formato vengono elaborati in sequenza dall'inizio della stringa. Ogni elemento di formato ha un indice che corrisponde a un oggetto nell'elenco di argomenti del metodo. Il Format metodo recupera l'argomento e deriva la relativa rappresentazione di stringa nel modo seguente:

Per un esempio che intercetta le chiamate al ICustomFormatter.Format metodo e consente di visualizzare le informazioni passate dal Format metodo a un metodo di formattazione per ogni elemento di formato in una stringa di formato composito, vedere Esempio: Provider di intercettazione e formattatore numerale romano.

Per altre informazioni, vedere Elaborazione dell'ordine.

Formattare gli elementi con lo stesso indice

Il Format metodo genera un'eccezione FormatException se l'indice di un elemento di indice è maggiore o uguale al numero di argomenti nell'elenco di argomenti. Tuttavia, format può includere più elementi di formato rispetto agli argomenti, purché più elementi di formato abbiano lo stesso indice. Nella chiamata al Format(String, Object) metodo nell'esempio seguente, l'elenco di argomenti ha un singolo argomento, ma la stringa di formato include due elementi di formato: uno visualizza il valore decimale di un numero e l'altro visualizza il relativo valore esadecimale.

short[] values= { Int16.MinValue, -27, 0, 1042, Int16.MaxValue };
Console.WriteLine("{0,10}  {1,10}\n", "Decimal", "Hex");
foreach (short value in values)
{
   string formatString = String.Format("{0,10:G}: {0,10:X}", value);
   Console.WriteLine(formatString);
}
// The example displays the following output:
//       Decimal         Hex
//
//        -32768:       8000
//           -27:       FFE5
//             0:          0
//          1042:        412
//         32767:       7FFF
open System

let values= [| Int16.MinValue; -27s; 0s; 1042s; Int16.MaxValue |]
printfn "%10s  %10s\n" "Decimal" "Hex"
for value in values do
    String.Format("{0,10:G}: {0,10:X}", value)
    |> printfn "%s"
// The example displays the following output:
//       Decimal         Hex
//    
//        -32768:       8000
//           -27:       FFE5
//             0:          0
//          1042:        412
//         32767:       7FFF
Module Example1
    Public Sub Main()
        Dim values() As Short = {Int16.MinValue, -27, 0, 1042, Int16.MaxValue}
        Console.WriteLine("{0,10}  {1,10}", "Decimal", "Hex")
        Console.WriteLine()
        For Each value As Short In values
            Dim formatString As String = String.Format("{0,10:G}: {0,10:X}", value)
            Console.WriteLine(formatString)
        Next
    End Sub
End Module
' The example displays the following output:
'       Decimal         Hex
'    
'        -32768:       8000
'           -27:       FFE5
'             0:          0
'          1042:        412
'         32767:       7FFF

Formato e cultura

In genere, gli oggetti nell'elenco di argomenti vengono convertiti nelle relative rappresentazioni di stringa usando le convenzioni della cultura attuale, restituite dalla proprietà CultureInfo.CurrentCulture. È possibile controllare questo comportamento chiamando uno dei sovraccarichi di Format che include un parametro provider. Il provider parametro è un'implementazione IFormatProvider che fornisce informazioni di formattazione specifiche della cultura e personalizzate, usate per gestire il processo di formattazione.

L'interfaccia IFormatProvider ha un singolo membro, GetFormat, responsabile della restituzione dell'oggetto che fornisce informazioni di formattazione. .NET include tre IFormatProvider implementazioni che offrono una formattazione specifica per cultura:

Operazioni di formattazione personalizzate

È anche possibile chiamare uno qualsiasi degli overload del metodo Format che hanno un parametro provider di tipo IFormatProvider per eseguire operazioni di formattazione personalizzate. Ad esempio, è possibile formattare un numero intero come numero di identificazione o come numero di telefono. Per eseguire la formattazione personalizzata, l'argomento provider deve implementare sia l'interfaccia IFormatProvider che l'interfaccia ICustomFormatter. Quando per il Format metodo viene fornita un'implementazione ICustomFormatter come provider argomento, il Format metodo chiama la sua implementazione IFormatProvider.GetFormat e richiede un oggetto di tipo ICustomFormatter. Chiama quindi il metodo ICustomFormatter dell'oggetto Format restituito per formattare ogni elemento di formato nella stringa composita passata.

Per altre informazioni sulla fornitura di soluzioni di formattazione personalizzate, vedere Procedura: Definire e usare provider di formati numerici personalizzati e ICustomFormatter. Per un esempio che converte numeri interi in numeri personalizzati formattati, vedere Esempio: Operazione di formattazione personalizzata. Per un esempio che converte i byte non firmati in numeri romani, vedere Esempio: provider di intercettazione e formattatore numerico romano.

Esempio: operazione di formattazione personalizzata

In questo esempio viene definito un provider di formato che formatta un valore intero come numero di account cliente nel formato x-xxxxx-xx.

using System;

public class TestFormatter
{
   public static void Main()
   {
      int acctNumber = 79203159;
      Console.WriteLine(String.Format(new CustomerFormatter(), "{0}", acctNumber));
      Console.WriteLine(String.Format(new CustomerFormatter(), "{0:G}", acctNumber));
      Console.WriteLine(String.Format(new CustomerFormatter(), "{0:S}", acctNumber));
      Console.WriteLine(String.Format(new CustomerFormatter(), "{0:P}", acctNumber));
      try {
         Console.WriteLine(String.Format(new CustomerFormatter(), "{0:X}", acctNumber));
      }
      catch (FormatException e) {
         Console.WriteLine(e.Message);
      }
   }
}

public class CustomerFormatter : IFormatProvider, ICustomFormatter
{
   public object GetFormat(Type formatType) 
   {
      if (formatType == typeof(ICustomFormatter))        
         return this; 
      else
         return null;
   }
   
   public string Format(string format, 
                         object arg, 
                         IFormatProvider formatProvider) 
   {                       
      if (! this.Equals(formatProvider))
      {
         return null;
      }
      else
      {
         if (String.IsNullOrEmpty(format)) 
            format = "G";
         
         string customerString = arg.ToString();
         if (customerString.Length < 8)
            customerString = customerString.PadLeft(8, '0');
         
         format = format.ToUpper();
         switch (format)
         {
            case "G":
               return customerString.Substring(0, 1) + "-" +
                                     customerString.Substring(1, 5) + "-" +
                                     customerString.Substring(6);
            case "S":                          
               return customerString.Substring(0, 1) + "/" +
                                     customerString.Substring(1, 5) + "/" +
                                     customerString.Substring(6);
            case "P":                          
               return customerString.Substring(0, 1) + "." +
                                     customerString.Substring(1, 5) + "." +
                                     customerString.Substring(6);
            default:
               throw new FormatException( 
                         String.Format("The '{0}' format specifier is not supported.", format));
         }
      }   
   }
}
// The example displays the following output:
//       7-92031-59
//       7-92031-59
//       7/92031/59
//       7.92031.59
//       The 'X' format specifier is not supported.
open System

type CustomerFormatter() = 
    interface IFormatProvider with
        member this.GetFormat(formatType) =
            if formatType = typeof<ICustomFormatter> then
                this
            else
                null

    interface ICustomFormatter with
        member this.Format(format, arg, formatProvider: IFormatProvider) = 
            if this.Equals formatProvider |> not then
                null
            else
                let format = 
                    if String.IsNullOrEmpty format then "G"
                    else format.ToUpper()
                
                let customerString = 
                    let s = string arg
                    if s.Length < 8 then
                        s.PadLeft(8, '0')
                    else s
                
                match format with
                | "G" ->
                    customerString.Substring(0, 1) + "-" +
                        customerString.Substring(1, 5) + "-" +
                        customerString.Substring 6
                | "S" ->                          
                    customerString.Substring(0, 1) + "/" +
                        customerString.Substring(1, 5) + "/" +
                        customerString.Substring 6
                | "P" ->                          
                    customerString.Substring(0, 1) + "." +
                        customerString.Substring(1, 5) + "." +
                        customerString.Substring 6
                | _ ->
                    raise (FormatException $"The '{format}' format specifier is not supported.")

let acctNumber = 79203159
String.Format(CustomerFormatter(), "{0}", acctNumber)
|> printfn "%s"
String.Format(CustomerFormatter(), "{0:G}", acctNumber)
|> printfn "%s"
String.Format(CustomerFormatter(), "{0:S}", acctNumber)
|> printfn "%s"
String.Format(CustomerFormatter(), "{0:P}", acctNumber)
|> printfn "%s"
try
    String.Format(CustomerFormatter(), "{0:X}", acctNumber)
    |> printfn "%s"
with :? FormatException as e ->
    printfn $"{e.Message}"

// The example displays the following output:
//       7-92031-59
//       7-92031-59
//       7/92031/59
//       7.92031.59
//       The 'X' format specifier is not supported.
Module TestFormatter
   Public Sub Main()
      Dim acctNumber As Integer = 79203159
      Console.WriteLine(String.Format(New CustomerFormatter, "{0}", acctNumber))
      Console.WriteLine(String.Format(New CustomerFormatter, "{0:G}", acctNumber))
      Console.WriteLine(String.Format(New CustomerFormatter, "{0:S}", acctNumber))
      Console.WriteLine(String.Format(New CustomerFormatter, "{0:P}", acctNumber))
      Try
         Console.WriteLine(String.Format(New CustomerFormatter, "{0:X}", acctNumber))
      Catch e As FormatException
         Console.WriteLine(e.Message)
      End Try   
   End Sub
End Module

Public Class CustomerFormatter : Implements IFormatProvider, ICustomFormatter
   Public Function GetFormat(type As Type) As Object  _
                   Implements IFormatProvider.GetFormat
      If type Is GetType(ICustomFormatter) Then
         Return Me
      Else
         Return Nothing
      End If
   End Function
   
   Public Function Format(fmt As String, _
                           arg As Object, _
                           formatProvider As IFormatProvider) As String _
                    Implements ICustomFormatter.Format
      If Not Me.Equals(formatProvider) Then
         Return Nothing
      Else
         If String.IsNullOrEmpty(fmt) Then fmt = "G"
         
         Dim customerString As String = arg.ToString()
         if customerString.Length < 8 Then _
            customerString = customerString.PadLeft(8, "0"c)
         
         Select Case fmt
            Case "G"
               Return customerString.Substring(0, 1) & "-" & _
                                     customerString.Substring(1, 5) & "-" & _
                                     customerString.Substring(6)
            Case "S"                         
               Return customerString.Substring(0, 1) & "/" & _
                                     customerString.Substring(1, 5) & "/" & _
                                     customerString.Substring(6)
            Case "P"
               Return customerString.Substring(0, 1) & "." & _
                                     customerString.Substring(1, 5) & "." & _
                                     customerString.Substring(6)
            Case Else
               Throw New FormatException( _
                         String.Format("The '{0}' format specifier is not supported.", fmt))
         End Select                                                     
      End If   
   End Function
End Class
' The example displays the following output:
'       7-92031-59
'       7-92031-59
'       7/92031/59
'       7.92031.59
'       The 'X' format specifier is not supported.

Esempio: un fornitore di intercettazione e un formattatore di numeri romani

Questo esempio definisce un provider di formato personalizzato che implementa le ICustomFormatter interfacce e IFormatProvider per eseguire due operazioni:

  • Visualizza i parametri passati alla relativa ICustomFormatter.Format implementazione. In questo modo è possibile vedere quali parametri il Format(IFormatProvider, String, Object[]) metodo passa all'implementazione di formattazione personalizzata per ogni oggetto che tenta di formattare. Questo può essere utile quando si esegue il debug dell'applicazione.
  • Se l'oggetto da formattare è un valore di byte senza segno che deve essere formattato utilizzando la stringa di formato standard "R", il formattatore personalizzato formatta il valore numerico come numero romano.
using System;
using System.Globalization;

public class InterceptProvider : IFormatProvider, ICustomFormatter
{
   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(ICustomFormatter))
         return this;
      else
         return null;
   }

   public string Format(String format, Object obj, IFormatProvider provider)
   {
      // Display information about method call.
      string formatString = format ?? "<null>";
      Console.WriteLine("Provider: {0}, Object: {1}, Format String: {2}",
                        provider.GetType().Name, obj ?? "<null>", formatString);

      if (obj == null) return String.Empty;

      // If this is a byte and the "R" format string, format it with Roman numerals.
      if (obj is Byte && formatString.ToUpper().Equals("R")) {
         Byte value = (Byte) obj;
         int remainder;
         int result;
         String returnString = String.Empty;

         // Get the hundreds digit(s)
         result = Math.DivRem(value, 100, out remainder);
         if (result > 0)
            returnString = new String('C', result);
         value = (Byte) remainder;
         // Get the 50s digit
         result = Math.DivRem(value, 50, out remainder);
         if (result == 1)
            returnString += "L";
         value = (Byte) remainder;
         // Get the tens digit.
         result = Math.DivRem(value, 10, out remainder);
         if (result > 0)
            returnString += new String('X', result);
         value = (Byte) remainder;
         // Get the fives digit.
         result = Math.DivRem(value, 5, out remainder);
         if (result > 0)
            returnString += "V";
         value = (Byte) remainder;
         // Add the ones digit.
         if (remainder > 0)
            returnString += new String('I', remainder);

         // Check whether we have too many X characters.
         int pos = returnString.IndexOf("XXXX");
         if (pos >= 0) {
            int xPos = returnString.IndexOf("L");
            if (xPos >= 0 & xPos == pos - 1)
               returnString = returnString.Replace("LXXXX", "XC");
            else
               returnString = returnString.Replace("XXXX", "XL");
         }
         // Check whether we have too many I characters
         pos = returnString.IndexOf("IIII");
         if (pos >= 0)
            if (returnString.IndexOf("V") >= 0)
               returnString = returnString.Replace("VIIII", "IX");
            else
               returnString = returnString.Replace("IIII", "IV");

         return returnString;
      }

      // Use default for all other formatting.
      if (obj is IFormattable)
         return ((IFormattable) obj).ToString(format, CultureInfo.CurrentCulture);
      else
         return obj.ToString();
   }
}

public class FormatExample12
{
   public static void Main()
   {
      int n = 10;
      double value = 16.935;
      DateTime day = DateTime.Now;
      InterceptProvider provider = new InterceptProvider();
      Console.WriteLine(String.Format(provider, "{0:N0}: {1:C2} on {2:d}\n", n, value, day));
      Console.WriteLine(String.Format(provider, "{0}: {1:F}\n", "Today: ",
                                      (DayOfWeek) DateTime.Now.DayOfWeek));
      Console.WriteLine(String.Format(provider, "{0:X}, {1}, {2}\n",
                                      (Byte) 2, (Byte) 12, (Byte) 199));
      Console.WriteLine(String.Format(provider, "{0:R}, {1:R}, {2:R}\n",
                                      (Byte) 2, (Byte) 12, (Byte) 199));
   }
}
// The example displays the following output:
//    Provider: InterceptProvider, Object: 10, Format String: N0
//    Provider: InterceptProvider, Object: 16.935, Format String: C2
//    Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
//    10: $16.94 on 1/31/2013
//
//    Provider: InterceptProvider, Object: Today: , Format String: <null>
//    Provider: InterceptProvider, Object: Thursday, Format String: F
//    Today: : Thursday
//
//    Provider: InterceptProvider, Object: 2, Format String: X
//    Provider: InterceptProvider, Object: 12, Format String: <null>
//    Provider: InterceptProvider, Object: 199, Format String: <null>
//    2, 12, 199
//
//    Provider: InterceptProvider, Object: 2, Format String: R
//    Provider: InterceptProvider, Object: 12, Format String: R
//    Provider: InterceptProvider, Object: 199, Format String: R
//    II, XII, CXCIX
open System
open System.Globalization

type InterceptProvider() =
    interface IFormatProvider with
        member this.GetFormat(formatType) =
            if formatType = typeof<ICustomFormatter> then
                this
            else
                null
    interface ICustomFormatter with
        member _.Format(format, obj, provider: IFormatProvider) = 
            // Display information about method call.
            let formatString =
                if format = null then "<null>" else format
            printfn $"Provider: {provider.GetType().Name}, Object: %A{obj}, Format String: %s{formatString}"
                                
            if obj = null then
                String.Empty
            else
                // If this is a byte and the "R" format string, format it with Roman numerals.
                match obj with
                | :? byte as value when formatString.ToUpper().Equals "R" -> 
                    let mutable returnString = String.Empty

                    // Get the hundreds digit(s)
                    let struct (result, remainder) = Math.DivRem(value, 100uy)
                    if result > 0uy then
                        returnString <- String('C', int result)
                    let value = byte remainder
                    // Get the 50s digit
                    let struct (result, remainder) = Math.DivRem(value, 50uy)
                    if result = 1uy then
                        returnString <- returnString + "L"
                    let value = byte remainder
                    // Get the tens digit.
                    let struct (result, remainder) = Math.DivRem(value, 10uy)
                    if result > 0uy then
                        returnString <- returnString + String('X', int result)
                    let value = byte remainder 
                    // Get the fives digit.
                    let struct (result, remainder) = Math.DivRem(value, 5uy)
                    if result > 0uy then
                        returnString <- returnString + "V"
                    let value = byte remainder
                    // Add the ones digit.
                    if remainder > 0uy then 
                        returnString <- returnString + String('I', int remainder)
                    
                    // Check whether we have too many X characters.
                    let pos = returnString.IndexOf "XXXX"
                    if pos >= 0 then
                        let xPos = returnString.IndexOf "L" 
                        returnString <-
                            if xPos >= 0 && xPos = pos - 1 then
                                returnString.Replace("LXXXX", "XC")
                            else
                                returnString.Replace("XXXX", "XL")   
                    // Check whether we have too many I characters
                    let pos = returnString.IndexOf "IIII"
                    if pos >= 0 then
                        returnString <-
                            if returnString.IndexOf "V" >= 0 then
                                returnString.Replace("VIIII", "IX")
                            else
                                returnString.Replace("IIII", "IV")    
                    returnString 

                // Use default for all other formatting.
                | :? IFormattable as x ->
                    x.ToString(format, CultureInfo.CurrentCulture)
                | _ ->
                    string obj

let n = 10
let value = 16.935
let day = DateTime.Now
let provider = InterceptProvider()
String.Format(provider, "{0:N0}: {1:C2} on {2:d}\n", n, value, day)
|> printfn "%s"
String.Format(provider, "{0}: {1:F}\n", "Today: ", DateTime.Now.DayOfWeek)
|> printfn "%s"
String.Format(provider, "{0:X}, {1}, {2}\n", 2uy, 12uy, 199uy)
|> printfn "%s"
String.Format(provider, "{0:R}, {1:R}, {2:R}\n", 2uy, 12uy, 199uy)
|> printfn "%s"
// The example displays the following output:
//    Provider: InterceptProvider, Object: 10, Format String: N0
//    Provider: InterceptProvider, Object: 16.935, Format String: C2
//    Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
//    10: $16.94 on 1/31/2013
//    
//    Provider: InterceptProvider, Object: Today: , Format String: <null>
//    Provider: InterceptProvider, Object: Thursday, Format String: F
//    Today: : Thursday
//    
//    Provider: InterceptProvider, Object: 2, Format String: X
//    Provider: InterceptProvider, Object: 12, Format String: <null>
//    Provider: InterceptProvider, Object: 199, Format String: <null>
//    2, 12, 199
//    
//    Provider: InterceptProvider, Object: 2, Format String: R
//    Provider: InterceptProvider, Object: 12, Format String: R
//    Provider: InterceptProvider, Object: 199, Format String: R
//    II, XII, CXCIX
Imports System.Globalization

Public Class InterceptProvider : Implements IFormatProvider, ICustomFormatter
   Public Function GetFormat(formatType As Type) As Object _
         Implements IFormatProvider.GetFormat
      If formatType Is GetType(ICustomFormatter) Then
         Return Me
      Else
         Return Nothing
      End If
   End Function

   Public Function Format(fmt As String, obj As Object, provider As IFormatProvider) As String _
         Implements ICustomFormatter.Format

      Dim formatString As String = If(fmt IsNot Nothing, fmt, "<null>")
      Console.WriteLine("Provider: {0}, Object: {1}, Format String: {2}",
                        provider, If(obj IsNot Nothing, obj, "<null>"), formatString)

      If obj Is Nothing Then Return String.Empty

      ' If this is a byte and the "R" format string, format it with Roman numerals.
      If TypeOf(obj) Is Byte AndAlso formatString.ToUpper.Equals("R") Then
         Dim value As Byte = CByte(obj)
         Dim remainder As Integer
         Dim result As Integer
         Dim returnString As String = String.Empty

         ' Get the hundreds digit(s)
         result = Math.DivRem(value, 100, remainder)
         If result > 0 Then returnString = New String("C"c, result)
         value = CByte(remainder)
         ' Get the 50s digit
         result = Math.DivRem(value, 50, remainder)
         If result = 1 Then returnString += "L"
         value = CByte(remainder)
         ' Get the tens digit.
         result = Math.DivRem(value, 10, remainder)
         If result > 0 Then returnString += New String("X"c, result)
         value = CByte(remainder)
         ' Get the fives digit.
         result = Math.DivRem(value, 5, remainder)
         If result > 0 Then returnString += "V"
         value = CByte(remainder)
         ' Add the ones digit.
         If remainder > 0 Then returnString += New String("I"c, remainder)

         ' Check whether we have too many X characters.
         Dim pos As Integer = returnString.IndexOf("XXXX")
         If pos >= 0 Then
            Dim xPos As Integer = returnString.IndexOf("L")
            If xPos >= 0 And xPos = pos - 1 Then
               returnString = returnString.Replace("LXXXX", "XC")
            Else
               returnString = returnString.Replace("XXXX", "XL")
            End If
         End If
         ' Check whether we have too many I characters
         pos = returnString.IndexOf("IIII")
         If pos >= 0 Then
            If returnString.IndexOf("V") >= 0 Then
               returnString = returnString.Replace("VIIII", "IX")
            Else
               returnString = returnString.Replace("IIII", "IV")
            End If
         End If
         Return returnString
      End If

      ' Use default for all other formatting.
      If TypeOf obj Is IFormattable Then
         Return CType(obj, IFormattable).ToString(fmt, CultureInfo.CurrentCulture)
      Else
         Return obj.ToString()
      End If
   End Function
End Class

Module Example
   Public Sub Main()
      Dim n As Integer = 10
      Dim value As Double = 16.935
      Dim day As DateTime = Date.Now
      Dim provider As New InterceptProvider()
      Console.WriteLine(String.Format(provider, "{0:N0}: {1:C2} on {2:d}", n, value, day))
      Console.WriteLine()
      Console.WriteLine(String.Format(provider, "{0}: {1:F}", "Today",
                                      CType(Date.Now.DayOfWeek, DayOfWeek)))
      Console.WriteLine()
      Console.WriteLine(String.Format(provider, "{0:X}, {1}, {2}\n",
                                      CByte(2), CByte(12), CByte(199)))
      Console.WriteLine()
      Console.WriteLine(String.Format(provider, "{0:R}, {1:R}, {2:R}",
                                      CByte(2), CByte(12), CByte(199)))
   End Sub
End Module
' The example displays the following output:
'    Provider: InterceptProvider, Object: 10, Format String: N0
'    Provider: InterceptProvider, Object: 16.935, Format String: C2
'    Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
'    10: $16.94 on 1/31/2013
'
'    Provider: InterceptProvider, Object: Today: , Format String: <null>
'    Provider: InterceptProvider, Object: Thursday, Format String: F
'    Today: : Thursday
'
'    Provider: InterceptProvider, Object: 2, Format String: X
'    Provider: InterceptProvider, Object: 12, Format String: <null>
'    Provider: InterceptProvider, Object: 199, Format String: <null>
'    2, 12, 199
'
'    Provider: InterceptProvider, Object: 2, Format String: R
'    Provider: InterceptProvider, Object: 12, Format String: R
'    Provider: InterceptProvider, Object: 199, Format String: R
'    II, XII, CXCIX

Domande frequenti

Perché è consigliabile eseguire l'interpolazione di stringhe sulle chiamate al String.Format metodo?

L'interpolazione di stringhe è:

  • Più flessibile. Può essere usato in qualsiasi stringa senza richiedere una chiamata a un metodo che supporta la formattazione composita. In caso contrario, è necessario chiamare il Format metodo o un altro metodo che supporta la formattazione composita, ad esempio Console.WriteLine o StringBuilder.AppendFormat.
  • Più leggibile. Poiché l'espressione da inserire in una stringa viene visualizzata nell'espressione interpolata anziché in un elenco di argomenti, le stringhe interpolate sono più facili da codificare e leggere. Le stringhe interpolate possono essere usate anche nelle operazioni di concatenazione di stringhe per produrre codice più conciso e più chiaro.

Un confronto dei due esempi di codice seguenti illustra la superiorità delle stringhe interpolate sulla concatenazione di stringhe e le chiamate ai metodi di formattazione composita. L'uso di più operazioni di concatenazione di stringhe nell'esempio seguente produce codice verbose e difficile da leggere.

string[] names = { "Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma" };
string output = names[0] + ", " + names[1] + ", " + names[2] + ", " +
                names[3] + ", " + names[4] + ", " + names[5] + ", " +
                names[6];

output += "\n";
var date = DateTime.Now;
output += String.Format("It is {0:t} on {0:d}. The day of the week is {1}.",
                        date, date.DayOfWeek);
Console.WriteLine(output);
// The example displays the following output:
//     Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
//     It is 10:29 AM on 1/8/2018. The day of the week is Monday.
open System

let names = [| "Balto"; "Vanya"; "Dakota"; "Samuel"; "Koani"; "Yiska"; "Yuma" |]
let output = 
    names[0] + ", " + names[1] + ", " + names[2] + ", " + 
    names[3] + ", " + names[4] + ", " + names[5] + ", " + 
    names[6] + "\n"

let date = DateTime.Now
output + String.Format("It is {0:t} on {0:d}. The day of the week is {1}.", date, date.DayOfWeek)
|> printfn "%s"
// The example displays the following output:
//     Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
//     It is 10:29 AM on 1/8/2018. The day of the week is Monday.

Module Example12
    Public Sub Main()
        Dim names = {"Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma"}
        Dim output = names(0) + ", " + names(1) + ", " + names(2) + ", " +
                   names(3) + ", " + names(4) + ", " + names(5) + ", " +
                   names(6)

        output += vbCrLf
        Dim dat = DateTime.Now
        output += String.Format("It is {0:t} on {0:d}. The day of the week is {1}.",
                              dat, dat.DayOfWeek)
        Console.WriteLine(output)
    End Sub
End Module
' The example displays the following output:
'     Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
'     It is 10:29 AM on 1/8/2018. The day of the week is Monday.

Al contrario, l'uso di stringhe interpolate nell'esempio seguente produce codice molto più chiaro e conciso rispetto all'istruzione di concatenazione di stringhe e alla chiamata al Format metodo nell'esempio precedente.

string[] names = { "Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma" };
string output = $"{names[0]}, {names[1]}, {names[2]}, {names[3]}, {names[4]}, " +
                $"{names[5]}, {names[6]}";

var date = DateTime.Now;
output += $"\nIt is {date:t} on {date:d}. The day of the week is {date.DayOfWeek}.";
Console.WriteLine(output);
// The example displays the following output:
//     Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
//     It is 10:29 AM on 1/8/2018. The day of the week is Monday.
open System

let names = [| "Balto"; "Vanya"; "Dakota"; "Samuel"; "Koani"; "Yiska"; "Yuma" |]
let output = $"{names[0]}, {names[1]}, {names[2]}, {names[3]}, {names[4]}, {names[5]}, {names[6]}"  

let date = DateTime.Now
output + $"\nIt is {date:t} on {date:d}. The day of the week is {date.DayOfWeek}."
|> printfn "%s" 
// The example displays the following output:
//     Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
//     It is 10:29 AM on 1/8/2018. The day of the week is Monday.

Module Example13
    Public Sub Main()
        Dim names = {"Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma"}
        Dim output = $"{names(0)}, {names(1)}, {names(2)}, {names(3)}, {names(4)}, " +
                   $"{names(5)}, {names(6)}"

        Dim dat = DateTime.Now
        output += $"{vbCrLf}It is {dat:t} on {dat:d}. The day of the week is {dat.DayOfWeek}."
        Console.WriteLine(output)
    End Sub
End Module
' The example displays the following output:
'     Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
'     It is 10:29 AM on 1/8/2018. The day of the week is Monday.

Dove è possibile trovare le stringhe di formato predefinite?

Come è possibile controllare l'allineamento delle stringhe dei risultati che sostituiscono gli elementi di formato?

La sintassi generale di un elemento di formato è:

{index[,width][: formatString]}

width è un intero con segno che definisce la larghezza del campo. Se questo valore è negativo, il testo nel campo è allineato a sinistra. Se è positivo, il testo è allineato a destra.

Come è possibile controllare il numero di cifre dopo il separatore decimale?

Tutte le stringhe di formato numerico standard ad eccezione di "D" (usate solo con numeri interi), "G", "R" e "X" consentono un identificatore di precisione che definisce il numero di cifre decimali nella stringa di risultato. Nell'esempio seguente vengono utilizzate stringhe di formato numerico standard per controllare il numero di cifre decimali nella stringa di risultato.

object[] values = { 1603, 1794.68235, 15436.14 };
string result;
foreach (var value in values)
{
    result = String.Format("{0,12:C2}   {0,12:E3}   {0,12:F4}   {0,12:N3}  {1,12:P2}\n",
                           Convert.ToDouble(value), Convert.ToDouble(value) / 10000);
    Console.WriteLine(result);
}
// The example displays output like the following:
//       $1,603.00     1.603E+003      1603.0000      1,603.000       16.03 %
//
//       $1,794.68     1.795E+003      1794.6824      1,794.682       17.95 %
//
//      $15,436.14     1.544E+004     15436.1400     15,436.140      154.36 %
open System

let values: obj list = [ 1603, 1794.68235, 15436.14 ]
for value in values do
   String.Format("{0,12:C2}   {0,12:E3}   {0,12:F4}   {0,12:N3}  {1,12:P2}\n", Convert.ToDouble(value), Convert.ToDouble(value) / 10000.)
   |> printfn "%s"
// The example displays output like the following:
//       $1,603.00     1.603E+003      1603.0000      1,603.000       16.03 %
//    
//       $1,794.68     1.795E+003      1794.6824      1,794.682       17.95 %
//    
//      $15,436.14     1.544E+004     15436.1400     15,436.140      154.36 %
Module Example7
    Public Sub Main()
        Dim values() As Object = {1603, 1794.68235, 15436.14}
        Dim result As String
        For Each value In values
            result = String.Format("{0,12:C2}   {0,12:E3}   {0,12:F4}   {0,12:N3}  {1,12:P2}",
                                value, CDbl(value) / 10000)
            Console.WriteLine(result)
            Console.WriteLine()
        Next
    End Sub
End Module
' The example displays the following output:
'       $1,603.00     1.603E+003      1603.0000      1,603.000       16.03 %
'    
'       $1,794.68     1.795E+003      1794.6824      1,794.682       17.95 %
'    
'      $15,436.14     1.544E+004     15436.1400     15,436.140      154.36 %

Se si usa una stringa di formato numerico personalizzata, usare l'identificatore di formato "0" per controllare il numero di cifre decimali nella stringa di risultato, come illustrato nell'esempio seguente.

decimal value = 16309.5436m;
string result = String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}",
                              value);
Console.WriteLine(result);
// The example displays the following output:
//        16309.54360    16,309.54    16309.544
let value = 16309.5436m
String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}", value)
|> printfn "%s"
// The example displays the following output:
//        16309.54360    16,309.54    16309.544
Module Example8
    Public Sub Main()
        Dim value As Decimal = 16309.5436D
        Dim result As String = String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}",
                                           value)
        Console.WriteLine(result)
    End Sub
End Module
' The example displays the following output:
'    16309.54360    16,309.54    16309.544

Come si controlla il numero di cifre integrali?

Per impostazione predefinita, le operazioni di formattazione visualizzano solo cifre integrali non zero. Se si formattano numeri interi, è possibile usare un identificatore di precisione con le stringhe di formato standard "D" e "X" per controllare il numero di cifre.

int value = 1326;
string result = String.Format("{0,10:D6} {0,10:X8}", value);
Console.WriteLine(result);
// The example displays the following output:
//     001326   0000052E
open System

let value = 1326
String.Format("{0,10:D6} {0,10:X8}", value)
|> printfn "%s"
// The example displays the following output:
//     001326   0000052E
Module Example10
    Public Sub Main()
        Dim value As Integer = 1326
        Dim result As String = String.Format("{0,10:D6} {0,10:X8}", value)
        Console.WriteLine(result)
    End Sub
End Module
' The example displays the following output:
'       001326   0000052E

È possibile inserire un numero intero o a virgola mobile con zeri iniziali per produrre una stringa di risultato con un numero specificato di cifre integrali usando l'identificatore di formato numerico personalizzato "0", come illustrato nell'esempio seguente.

int value = 16342;
string result = String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}",
                              value);
Console.WriteLine(result);
// The example displays the following output:
//           00016342       00016342.000    0,000,016,342.0
open System

let value = 16342
String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}", value)
|> printfn "%s"
// The example displays the following output:
//           00016342       00016342.000    0,000,016,342.0
Module Example9
    Public Sub Main()
        Dim value As Integer = 16342
        Dim result As String = String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}",
                                           value)
        Console.WriteLine(result)
    End Sub
End Module
' The example displays the following output:
'           00016342       00016342.000    0,000,016,342.0

Quanti elementi è possibile includere nell'elenco di formato?

Non esiste alcun limite pratico. Il secondo parametro del Format(IFormatProvider, String, Object[]) metodo viene contrassegnato con l'attributo ParamArrayAttribute , che consente di includere un elenco delimitato o una matrice di oggetti come elenco di formati.

Come posso includere le parentesi graffe letterali ("{" e "}") nella stringa di risultato?

Ad esempio, come si impedisce che la chiamata al metodo seguente generi un'eccezione FormatException ?

result = String.Format("The text has {0} '{' characters and {1} '}' characters.",
                       nOpen, nClose);
let result = 
    String.Format("The text has {0} '{' characters and {1} '}' characters.", nOpen, nClose)
result = String.Format("The text has {0} '{' characters and {1} '}' characters.",
                 nOpen, nClose)

Una singola parentesi graffa di apertura o chiusura è sempre interpretata come l'inizio o la fine dell'elemento di formato. Per essere interpretato letteralmente, deve essere scappato. È possibile eseguire l'escape di una parentesi graffa aggiungendo un'altra parentesi graffa ("{{" e "}}" anziché "{" e "}"), come nella chiamata al metodo seguente:

string result;
int nOpen = 1;
int nClose = 2;
result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
                       nOpen, nClose);
Console.WriteLine(result);
let result =
    String.Format("The text has {0} '{{' characters and {1} '}}' characters.", nOpen, nClose)
result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
                 nOpen, nClose)

Tuttavia, anche le parentesi graffe precedute da escape sono facilmente interpretate erroneamente. È consigliabile includere parentesi graffe nell'elenco di formato e usare gli elementi di formato per inserirli nella stringa del risultato, come illustrato nell'esempio seguente.

string result;
int nOpen = 1;
int nClose = 2;
result = String.Format("The text has {0} '{1}' characters and {2} '{3}' characters.",
                       nOpen, "{", nClose, "}");
Console.WriteLine(result);
let result =
    String.Format("The text has {0} '{1}' characters and {2} '{3}' characters.", nOpen, "{", nClose, "}")
result = String.Format("The text has {0} '{1}' characters and {2} '{3}' characters.",
                 nOpen, "{", nClose, "}")

Perché la chiamata al metodo String.Format genera un'eccezione FormatException?

La causa più comune dell'eccezione è che l'indice di un elemento di formato non corrisponde a un oggetto nell'elenco di formato. In genere ciò indica che sono stati numerati erroneamente gli indici degli elementi di formato o che si è dimenticato di includere un oggetto nell'elenco di formato. Il tentativo di includere un carattere di parentesi graffa sinistra o destra senza eseguire l'escape genera anche un'eccezione FormatException. Occasionalmente, l'eccezione è il risultato di un errore di digitatura; Ad esempio, un errore tipico consiste nel digitare erroneamente "[" (parentesi quadra sinistra) anziché "{" (parentesi graffa sinistra).

Se il metodo Format(System.IFormatProvider,System.String,System.Object[]) supporta matrici di parametri, perché il codice genera un'eccezione quando si usa una matrice?

Ad esempio, il codice seguente genera un'eccezione FormatException :

Random rnd = new Random();
int[] numbers = new int[4];
int total = 0;
for (int ctr = 0; ctr <= 2; ctr++)
{
    int number = rnd.Next(1001);
    numbers[ctr] = number;
    total += number;
}
numbers[3] = total;
Console.WriteLine($"{numbers} + {1} + {2} = {3}");
open System

let rnd = Random()
let mutable total = 0
let numbers = Array.zeroCreate<int> 4
for i = 0 to 2 do
   let number = rnd.Next 1001
   numbers[i] <- number
   total <- total + number
numbers[3] <- total
Console.WriteLine("{0} + {1} + {2} = {3}", numbers)

Module Example5a
    Public Sub Main()
        Dim rnd As New Random()
        Dim numbers(3) As Integer
        Dim total As Integer = 0
        For ctr = 0 To 2
            Dim number As Integer = rnd.Next(1001)
            numbers(ctr) = number
            total += number
        Next
        numbers(3) = total
        Console.WriteLine("{0} + {1} + {2} = {3}", numbers)
    End Sub
End Module

Si tratta di un problema di risoluzione dell'overload del compilatore. Poiché il compilatore non è in grado di convertire una matrice di numeri interi in una matrice di oggetti, considera la matrice integer come un singolo argomento, quindi chiama il Format(String, Object) metodo . L'eccezione viene generata perché sono presenti quattro elementi di formato, ma solo un singolo elemento nell'elenco di formato.

Poiché né Visual Basic né C# possono convertire una matrice integer in una matrice di oggetti, è necessario eseguire manualmente la conversione prima di chiamare il Format(String, Object[]) metodo . Nell'esempio seguente viene fornita un'implementazione.

Random rnd = new Random();
int[] numbers = new int[4];
int total = 0;
for (int ctr = 0; ctr <= 2; ctr++)
{
    int number = rnd.Next(1001);
    numbers[ctr] = number;
    total += number;
}
numbers[3] = total;
object[] values = new object[numbers.Length];
numbers.CopyTo(values, 0);
Console.WriteLine($"{values} + {1} + {2} = {3}");
open System

let rnd = Random()
let numbers = Array.zeroCreate<int> 4
let mutable total = 0
for i = 0 to 2 do
   let number = rnd.Next 1001
   numbers[i] <- number
   total <- total + number
numbers[3] <- total
let values = Array.zeroCreate<obj> numbers.Length
numbers.CopyTo(values, 0)
Console.WriteLine("{0} + {1} + {2} = {3}", values)
Imports System.Collections.Generic

Module Example6
    Public Sub Main()
        Dim rnd As New Random()
        Dim numbers(3) As Integer
        Dim total As Integer = 0
        For ctr = 0 To 2
            Dim number As Integer = rnd.Next(1001)
            numbers(ctr) = number
            total += number
        Next
        numbers(3) = total
        Dim values(numbers.Length - 1) As Object
        numbers.CopyTo(values, 0)
        Console.WriteLine("{0} + {1} + {2} = {3}", values)
    End Sub
End Module

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Format(IFormatProvider, String, Object, Object, Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce gli elementi di formato in una stringa con la rappresentazione di stringa di tre oggetti specificati. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static string Format(IFormatProvider provider, string format, object arg0, object arg1, object arg2);
public static string Format(IFormatProvider? provider, string format, object? arg0, object? arg1, object? arg2);
static member Format : IFormatProvider * string * obj * obj * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object, arg2 As Object) As String

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
String

Stringa di formato composito.

arg0
Object

Primo oggetto da formattare.

arg1
Object

Secondo oggetto da formattare.

arg2
Object

Terzo oggetto da formattare.

Valori restituiti

Copia di in cui gli elementi di format formato sono stati sostituiti dalle rappresentazioni di stringa di arg0, arg1e arg2.

Eccezioni

format è null.

format non è valido.

oppure

L'indice di un elemento di formato è minore di zero o maggiore di due.

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Questo metodo usa la funzionalità di formattazione composita per convertire tre espressioni nelle relative rappresentazioni di stringa e per incorporare tali rappresentazioni in una stringa. Nell'esecuzione della conversione, il metodo usa la formattazione sensibile alle impostazioni cultura o un formattatore personalizzato. Il metodo converte ogni Object argomento nella rappresentazione di stringa chiamando il metodo ToString(IFormatProvider) o, se l'elemento di formato corrispondente dell'oggetto include una stringa di formato, chiamando il relativo metodo ToString(String,IFormatProvider). Se questi metodi non esistono, chiama il metodo ToString senza parametri dell'oggetto.

Tuttavia, quando si chiama il String.Format metodo, non è necessario concentrarsi sull'overload specifico che si desidera chiamare. È invece possibile chiamare il metodo con un oggetto che fornisce formattazione personalizzata o sensibile alle impostazioni cultura e una stringa di formato composito che include uno o più elementi di formato. Assegnare a ogni elemento di formato un indice numerico; il primo indice inizia da 0. Oltre alla stringa iniziale, la chiamata al metodo deve avere un numero di argomenti aggiuntivi pari a quello dei valori di indice. Ad esempio, una stringa i cui elementi di formato hanno indici pari a 0 e 1 devono avere 2 argomenti; uno con indici da 0 a 5 deve avere 6 argomenti. Il compilatore del linguaggio risolverà quindi la chiamata al metodo a un particolare overload del String.Format metodo .

Per una documentazione più dettagliata sull'uso del String.Format metodo , vedere Introduzione al metodo String.Format e Quale metodo chiamare?.

Si applica a

Format(IFormatProvider, String, Object, Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce gli elementi di formato in una stringa con la rappresentazione di stringa di due oggetti specificati. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static string Format(IFormatProvider provider, string format, object arg0, object arg1);
public static string Format(IFormatProvider? provider, string format, object? arg0, object? arg1);
static member Format : IFormatProvider * string * obj * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object) As String

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
String

Stringa di formato composito.

arg0
Object

Primo oggetto da formattare.

arg1
Object

Secondo oggetto da formattare.

Valori restituiti

Copia di in cui gli elementi di format formato vengono sostituiti dalle rappresentazioni di stringa di arg0 e arg1.

Eccezioni

format è null.

format non è valido.

oppure

L'indice di un elemento di formato non è zero o uno.

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Questo metodo usa la funzionalità di formattazione composita per convertire due espressioni nelle relative rappresentazioni di stringa e per incorporare tali rappresentazioni in una stringa. Nell'esecuzione della conversione, il metodo usa la formattazione sensibile alle impostazioni cultura o un formattatore personalizzato. Il metodo converte ogni Object argomento nella rappresentazione di stringa chiamando il metodo ToString(IFormatProvider) o, se l'elemento di formato corrispondente dell'oggetto include una stringa di formato, chiamando il relativo metodo ToString(String,IFormatProvider). Se questi metodi non esistono, chiama il metodo ToString senza parametri dell'oggetto.

Tuttavia, quando si chiama il String.Format metodo, non è necessario concentrarsi sull'overload specifico che si desidera chiamare. È invece possibile chiamare il metodo con un oggetto che fornisce formattazione personalizzata o sensibile alle impostazioni cultura e una stringa di formato composito che include uno o più elementi di formato. Assegnare a ogni elemento di formato un indice numerico; il primo indice inizia da 0. Oltre alla stringa iniziale, la chiamata al metodo deve avere un numero di argomenti aggiuntivi pari a quello dei valori di indice. Ad esempio, una stringa i cui elementi di formato hanno indici pari a 0 e 1 devono avere 2 argomenti; uno con indici da 0 a 5 deve avere 6 argomenti. Il compilatore del linguaggio risolverà quindi la chiamata al metodo a un particolare overload del String.Format metodo .

Per una documentazione più dettagliata sull'uso del String.Format metodo , vedere Introduzione al metodo String.Format e Quale metodo chiamare?.

Si applica a

Format(String, Object, Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce gli elementi di formato in una stringa con la rappresentazione di stringa di due oggetti specificati.

public:
 static System::String ^ Format(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static string Format(string format, object arg0, object arg1);
public static string Format(string format, object? arg0, object? arg1);
static member Format : string * obj * obj -> string
Public Shared Function Format (format As String, arg0 As Object, arg1 As Object) As String

Parametri

format
String

Stringa di formato composito.

arg0
Object

Primo oggetto da formattare.

arg1
Object

Secondo oggetto da formattare.

Valori restituiti

Copia di in cui gli elementi di format formato vengono sostituiti dalle rappresentazioni di stringa di arg0 e arg1.

Eccezioni

format è null.

format non è valido.

oppure

L'indice di un elemento di formato non è zero o uno.

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Questo metodo usa la funzionalità di formattazione composita per convertire il valore di due espressioni nelle relative rappresentazioni di stringa e per incorporare tali rappresentazioni in una stringa.

Tuttavia, quando si chiama il String.Format metodo, non è necessario concentrarsi sull'overload specifico che si desidera chiamare. È invece possibile chiamare il metodo con una stringa di formato composito che include uno o più elementi di formato. Assegnare a ogni elemento di formato un indice numerico; il primo indice inizia da 0. Oltre alla stringa iniziale, la chiamata al metodo deve avere un numero di argomenti aggiuntivi pari a quello dei valori di indice. Ad esempio, una stringa i cui elementi di formato hanno indici pari a 0 e 1 devono avere 2 argomenti; uno con indici da 0 a 5 deve avere 6 argomenti. Il compilatore del linguaggio risolverà quindi la chiamata al metodo a un particolare overload del String.Format metodo .

Per una documentazione più dettagliata sull'uso del String.Format metodo , vedere Introduzione al metodo String.Format e Quale metodo chiamare?.

Esempio: Formattare due argomenti

In questo esempio viene utilizzato il Format(String, Object, Object) metodo per visualizzare i dati relativi all'ora e alla temperatura archiviati in un oggetto generico Dictionary<TKey,TValue> . Si noti che la stringa di formato ha tre elementi di formato, anche se sono presenti solo due oggetti da formattare. Ciò avviene perché il primo oggetto nell'elenco (un valore di data e ora) viene utilizzato da due elementi di formato: il primo elemento di formato visualizza l'ora e il secondo visualizza la data.

Dictionary<DateTime, Double> temperatureInfo = new Dictionary<DateTime, Double>();
temperatureInfo.Add(new DateTime(2010, 6, 1, 14, 0, 0), 87.46);
temperatureInfo.Add(new DateTime(2010, 12, 1, 10, 0, 0), 36.81);

Console.WriteLine("Temperature Information:\n");
string output;
foreach (var item in temperatureInfo)
{
   output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F",
                          item.Key, item.Value);
   Console.WriteLine(output);
}
// The example displays output like the following:
//       Temperature Information:
//
//       Temperature at  2:00 PM on  6/1/2010:  87.5°F
//       Temperature at 10:00 AM on 12/1/2010:  36.8°F
open System
open System.Collections.Generic

let temperatureInfo = Dictionary<DateTime, float>() 
temperatureInfo.Add(DateTime(2010, 6, 1, 14, 0, 0), 87.46)
temperatureInfo.Add(DateTime(2010, 12, 1, 10, 0, 0), 36.81)

printfn $"Temperature Information:\n"
for item in temperatureInfo do
   String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", item.Key, item.Value)
   |> printfn "%s"
// The example displays output like the following:
//       Temperature Information:
//       
//       Temperature at  2:00 PM on  6/1/2010:  87.5°F
//       Temperature at 10:00 AM on 12/1/2010:  36.8°F
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim temperatureInfo As New Dictionary(Of Date, Double) 
      temperatureInfo.Add(#6/1/2010 2:00PM#, 87.46)
      temperatureInfo.Add(#12/1/2010 10:00AM#, 36.81)
      
      Console.WriteLine("Temperature Information:")
      Console.WriteLine()
      Dim output As String   
      For Each item In temperatureInfo
         output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", _
                                item.Key, item.Value)
         Console.WriteLine(output)
      Next
   End Sub
End Module
' The example displays the following output:
'       Temperature Information:
'       
'       Temperature at  2:00 PM on  6/1/2010:  87.5°F
'       Temperature at 10:00 AM on 12/1/2010:  36.8°F

Vedi anche

Si applica a

Format(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format(IFormatProvider? provider, System.Text.CompositeFormat format, scoped ReadOnlySpan<object?> args);
public static string Format(IFormatProvider? provider, System.Text.CompositeFormat format, ReadOnlySpan<object?> args);
static member Format : IFormatProvider * System.Text.CompositeFormat * ReadOnlySpan<obj> -> string
Public Shared Function Format (provider As IFormatProvider, format As CompositeFormat, args As ReadOnlySpan(Of Object)) As String

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
CompositeFormat

Un oggetto CompositeFormat.

args
ReadOnlySpan<Object>

Intervallo di oggetti da formattare.

Valori restituiti

Stringa formattata.

Eccezioni

format è null.

L'indice di un elemento di formato è maggiore o uguale al numero di argomenti forniti.

Si applica a

Format(IFormatProvider, CompositeFormat, Object[])

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format(IFormatProvider? provider, System.Text.CompositeFormat format, params object?[] args);
static member Format : IFormatProvider * System.Text.CompositeFormat * obj[] -> string
Public Shared Function Format (provider As IFormatProvider, format As CompositeFormat, ParamArray args As Object()) As String

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
CompositeFormat

Un oggetto CompositeFormat.

args
Object[]

Matrice di oggetti da formattare.

Valori restituiti

Stringa formattata.

Eccezioni

format o args è null.

L'indice di un elemento di formato è maggiore o uguale al numero di argomenti forniti.

Si applica a

Format(String, Object, Object, Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce gli elementi di formato in una stringa con la rappresentazione di stringa di tre oggetti specificati.

public:
 static System::String ^ Format(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static string Format(string format, object arg0, object arg1, object arg2);
public static string Format(string format, object? arg0, object? arg1, object? arg2);
static member Format : string * obj * obj * obj -> string
Public Shared Function Format (format As String, arg0 As Object, arg1 As Object, arg2 As Object) As String

Parametri

format
String

Stringa di formato composito.

arg0
Object

Primo oggetto da formattare.

arg1
Object

Secondo oggetto da formattare.

arg2
Object

Terzo oggetto da formattare.

Valori restituiti

Copia di in cui gli elementi di format formato sono stati sostituiti dalle rappresentazioni di stringa di arg0, arg1e arg2.

Eccezioni

format è null.

format non è valido.

oppure

L'indice di un elemento di formato è minore di zero o maggiore di due.

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Questo metodo usa la funzionalità di formattazione composita per convertire il valore di tre espressioni nelle relative rappresentazioni di stringa e per incorporare tali rappresentazioni in una stringa.

Tuttavia, quando si chiama il String.Format metodo, non è necessario concentrarsi sull'overload specifico che si desidera chiamare. È invece possibile chiamare il metodo con una stringa di formato composito che include uno o più elementi di formato. Assegnare a ogni elemento di formato un indice numerico; il primo indice inizia da 0. Oltre alla stringa iniziale, la chiamata al metodo deve avere un numero di argomenti aggiuntivi pari a quello dei valori di indice. Ad esempio, una stringa i cui elementi di formato hanno indici pari a 0 e 1 devono avere 2 argomenti; uno con indici da 0 a 5 deve avere 6 argomenti. Il compilatore del linguaggio risolverà quindi la chiamata al metodo a un particolare overload del String.Format metodo .

Per una documentazione più dettagliata sull'uso del String.Format metodo , vedere Introduzione al metodo String.Format e Quale metodo chiamare?.

Esempio: Formattare tre argomenti

In questo esempio viene utilizzato il Format(String, Object, Object, Object) metodo per creare una stringa che illustra il risultato di un'operazione booleana And con due valori interi. Si noti che la stringa di formato include sei elementi di formato, ma il metodo include solo tre elementi nell'elenco dei parametri, perché ogni elemento viene formattato in due modi diversi.

string formatString = "    {0,10} ({0,8:X8})\n" +
                      "And {1,10} ({1,8:X8})\n" +
                      "  = {2,10} ({2,8:X8})";
int value1 = 16932;
int value2 = 15421;
string result = string.Format(formatString,
                              value1, value2, value1 & value2);
Console.WriteLine(result);

// The example displays the following output:
//                16932 (00004224)
//       And      15421 (00003C3D)
//         =         36 (00000024)
open System

let formatString = 
    "    {0,10} ({0,8:X8})\nAnd {1,10} ({1,8:X8})\n  = {2,10} ({2,8:X8})"

let value1 = 16932
let value2 = 15421
String.Format(formatString, value1, value2, value1 &&& value2)
|> printfn "%s"
// The example displays the following output:
//                16932 (00004224)
//       And      15421 (00003C3D)
//         =         36 (00000024)
Public Module Example
   Public Sub Main()
      Dim formatString As String = "    {0,10} ({0,8:X8})" + vbCrLf +  _
                                   "And {1,10} ({1,8:X8})" + vbCrLf + _
                                   "  = {2,10} ({2,8:X8})"
      Dim value1 As Integer = 16932
      Dim value2 As Integer = 15421
      Dim result As String = String.Format(formatString, _
                                           value1, value2, value1 And value2)
      Console.WriteLine(result)                          
   End Sub
End Module
' The example displays the following output:
'                16932 (00004224)
'       And      15421 (00003C3D)
'         =         36 (00000024)

Vedi anche

Si applica a

Format(IFormatProvider, String, Object[])

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce gli elementi di formato in una stringa con le rappresentazioni di stringa degli oggetti corrispondenti in una matrice specificata. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format(IFormatProvider provider, string format, params object[] args);
public static string Format(IFormatProvider? provider, string format, params object?[] args);
static member Format : IFormatProvider * string * obj[] -> string
Public Shared Function Format (provider As IFormatProvider, format As String, ParamArray args As Object()) As String

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
String

Stringa di formato composito.

args
Object[]

Matrice di oggetti contenente zero o più oggetti da formattare.

Valori restituiti

Copia di format in cui gli elementi di formato sono stati sostituiti dalla rappresentazione di stringa degli oggetti corrispondenti in args.

Eccezioni

format o args è null.

format non è valido.

oppure

L'indice di un elemento di formato è minore di zero o maggiore o uguale alla lunghezza della args matrice.

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Questo metodo usa la funzionalità di formattazione composita per convertire quattro o più espressioni nelle relative rappresentazioni di stringa e per incorporare tali rappresentazioni in una stringa. Nell'esecuzione della conversione, il metodo usa la formattazione sensibile alle impostazioni cultura o un formattatore personalizzato. Il metodo converte ogni Object argomento nella rappresentazione di stringa chiamando il metodo ToString(IFormatProvider) o, se l'elemento di formato corrispondente dell'oggetto include una stringa di formato, chiamando il relativo metodo ToString(String,IFormatProvider). Se questi metodi non esistono, chiama il metodo ToString senza parametri dell'oggetto.

Tuttavia, quando si chiama il String.Format metodo, non è necessario concentrarsi sull'overload specifico che si desidera chiamare. È invece possibile chiamare il metodo con un oggetto che fornisce formattazione personalizzata o sensibile alle impostazioni cultura e una stringa di formato composito che include uno o più elementi di formato. Assegnare a ogni elemento di formato un indice numerico; il primo indice inizia da 0. Oltre alla stringa iniziale, la chiamata al metodo deve avere un numero di argomenti aggiuntivi pari a quello dei valori di indice. Ad esempio, una stringa i cui elementi di formato hanno indici pari a 0 e 1 devono avere 2 argomenti; uno con indici da 0 a 5 deve avere 6 argomenti. Il compilatore del linguaggio risolverà quindi la chiamata al metodo a un particolare overload del String.Format metodo .

Per una documentazione più dettagliata sull'uso del String.Format metodo , vedere Introduzione al metodo String.Format e Quale metodo chiamare?.

Esempio: Formattazione sensibile alle impostazioni cultura

In questo esempio viene utilizzato il Format(IFormatProvider, String, Object[]) metodo per visualizzare la rappresentazione di stringa di alcuni valori di data e ora e valori numerici usando diverse impostazioni cultura.

string[] cultureNames = { "en-US", "fr-FR", "de-DE", "es-ES" };

DateTime dateToDisplay = new DateTime(2009, 9, 1, 18, 32, 0);
double value = 9164.32;

Console.WriteLine("Culture     Date                                Value\n");
foreach (string cultureName in cultureNames)
{
   System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
   string output = String.Format(culture, "{0,-11} {1,-35:D} {2:N}",
                                 culture.Name, dateToDisplay, value);
   Console.WriteLine(output);
}
// The example displays the following output:
//    Culture     Date                                Value
//
//    en-US       Tuesday, September 01, 2009         9,164.32
//    fr-FR       mardi 1 septembre 2009              9 164,32
//    de-DE       Dienstag, 1. September 2009         9.164,32
//    es-ES       martes, 01 de septiembre de 2009    9.164,32
open System
open System.Globalization

let cultureNames = [| "en-US"; "fr-FR"; "de-DE"; "es-ES" |]

let dateToDisplay = DateTime(2009, 9, 1, 18, 32, 0)
let value = 9164.32

printfn "Culture     Date                                Value\n"
for cultureName in cultureNames do
    let culture = CultureInfo cultureName
    String.Format(culture, "{0,-11} {1,-35:D} {2:N}", culture.Name, dateToDisplay, value)
    |> printfn "%s"
// The example displays the following output:
//    Culture     Date                                Value
//    
//    en-US       Tuesday, September 01, 2009         9,164.32
//    fr-FR       mardi 1 septembre 2009              9 164,32
//    de-DE       Dienstag, 1. September 2009         9.164,32
//    es-ES       martes, 01 de septiembre de 2009    9.164,32
Imports System.Globalization

Module Example2
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "fr-FR", "de-DE", "es-ES" }

      Dim dateToDisplay As Date = #9/1/2009 6:32PM#
      Dim value As Double = 9164.32

      Console.WriteLine("Culture     Date                                Value")
      Console.WriteLine()
      For Each cultureName As String In cultureNames
         Dim culture As New CultureInfo(cultureName)
         Dim output As String = String.Format(culture, "{0,-11} {1,-35:D} {2:N}", _
                                              culture.Name, dateToDisplay, value)
         Console.WriteLine(output)
      Next
   End Sub
End Module
' The example displays the following output:
'       Culture     Date                                Value
'
'       en-US       Tuesday, September 01, 2009         9,164.32
'       fr-FR       mardi 1 septembre 2009              9 164,32
'       de-DE       Dienstag, 1. September 2009         9.164,32
'       es-ES       martes, 01 de septiembre de 2009    9.164,32

Vedi anche

Si applica a

Format(IFormatProvider, String, Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce l'elemento di formato o gli elementi in una stringa specificata con la rappresentazione di stringa dell'oggetto corrispondente. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0);
public static string Format(IFormatProvider provider, string format, object arg0);
public static string Format(IFormatProvider? provider, string format, object? arg0);
static member Format : IFormatProvider * string * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object) As String

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
String

Stringa di formato composito.

arg0
Object

Oggetto da formattare.

Valori restituiti

Copia di format in cui l'elemento di formato o gli elementi sono stati sostituiti dalla rappresentazione di stringa di arg0.

Eccezioni

format è null.

format non è valido.

oppure

L'indice di un elemento di formato non è zero.

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Questo metodo usa la funzionalità di formattazione composita per convertire il valore di un'espressione nella relativa rappresentazione di stringa e incorporare tale rappresentazione in una stringa. Nell'esecuzione della conversione, il metodo usa la formattazione sensibile alle impostazioni cultura o un formattatore personalizzato. Il metodo esegue la conversione arg0 nella rappresentazione di stringa chiamando il metodo ToString(IFormatProvider) o, se l'elemento di formato corrispondente dell'oggetto include una stringa di formato, chiamando il relativo metodo ToString(String,IFormatProvider). Se questi metodi non esistono, chiama il metodo ToString senza parametri dell'oggetto.

Tuttavia, quando si chiama il String.Format metodo, non è necessario concentrarsi sull'overload specifico che si desidera chiamare. È invece possibile chiamare il metodo con un oggetto che fornisce formattazione personalizzata o sensibile alle impostazioni cultura e una stringa di formato composito che include uno o più elementi di formato. Assegnare a ogni elemento di formato un indice numerico; il primo indice inizia da 0. Oltre alla stringa iniziale, la chiamata al metodo deve avere un numero di argomenti aggiuntivi pari a quello dei valori di indice. Ad esempio, una stringa i cui elementi di formato hanno indici pari a 0 e 1 devono avere 2 argomenti; uno con indici da 0 a 5 deve avere 6 argomenti. Il compilatore del linguaggio risolverà quindi la chiamata al metodo a un particolare overload del String.Format metodo .

Per una documentazione più dettagliata sull'uso del String.Format metodo , vedere Introduzione al metodo String.Format e Quale metodo chiamare?.

Si applica a

Format(String, ReadOnlySpan<Object>)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce l'elemento di formato in una stringa specificata con la rappresentazione di stringa di un oggetto corrispondente in un intervallo specificato.

public:
 static System::String ^ Format(System::String ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format(string format, scoped ReadOnlySpan<object?> args);
static member Format : string * ReadOnlySpan<obj> -> string
Public Shared Function Format (format As String, args As ReadOnlySpan(Of Object)) As String

Parametri

format
String

Stringa di formato composito.

args
ReadOnlySpan<Object>

Intervallo di oggetti che contiene zero o più oggetti da formattare.

Valori restituiti

Copia di format in cui gli elementi di formato sono stati sostituiti dalla rappresentazione di stringa degli oggetti corrispondenti in args.

Si applica a

Format(String, Object[])

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce l'elemento di formato in una stringa specificata con la rappresentazione di stringa di un oggetto corrispondente in una matrice specificata.

public:
 static System::String ^ Format(System::String ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format(string format, params object[] args);
public static string Format(string format, params object?[] args);
static member Format : string * obj[] -> string
Public Shared Function Format (format As String, ParamArray args As Object()) As String

Parametri

format
String

Stringa di formato composito.

args
Object[]

Matrice di oggetti contenente zero o più oggetti da formattare.

Valori restituiti

Copia di format in cui gli elementi di formato sono stati sostituiti dalla rappresentazione di stringa degli oggetti corrispondenti in args.

Eccezioni

format o args è null.

format non è valido.

oppure

L'indice di un elemento di formato è minore di zero o maggiore o uguale alla lunghezza della args matrice.

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Questo metodo usa la funzionalità di formattazione composita per convertire il valore di quattro o più espressioni nelle relative rappresentazioni di stringa e per incorporare tali rappresentazioni in una stringa. Poiché il args parametro è contrassegnato con l'attributo System.ParamArrayAttribute , è possibile passare gli oggetti al metodo come singoli argomenti o come Object matrice.

Tuttavia, quando si chiama il String.Format metodo, non è necessario concentrarsi sull'overload specifico che si desidera chiamare. È invece possibile chiamare il metodo con una stringa di formato composito che include uno o più elementi di formato. Assegnare a ogni elemento di formato un indice numerico; il primo indice inizia da 0. Oltre alla stringa iniziale, la chiamata al metodo deve avere un numero di argomenti aggiuntivi pari a quello dei valori di indice. Ad esempio, una stringa i cui elementi di formato hanno indici pari a 0 e 1 devono avere 2 argomenti; uno con indici da 0 a 5 deve avere 6 argomenti. Il compilatore del linguaggio risolverà quindi la chiamata al metodo a un particolare overload del String.Format metodo .

Per una documentazione più dettagliata sull'uso del String.Format metodo , vedere Introduzione al metodo String.Format e Quale metodo chiamare?.

Esempio: Formattare più di tre argomenti

In questo esempio viene creata una stringa contenente dati sulla temperatura elevata e bassa in una determinata data. La stringa di formato composito include cinque elementi di formato nell'esempio C# e sei nell'esempio di Visual Basic. Due degli elementi di formato definiscono la larghezza della rappresentazione di stringa del valore corrispondente e il primo elemento di formato include anche una stringa di formato di data e ora standard.

DateTime date1 = new DateTime(2009, 7, 1);
TimeSpan hiTime = new TimeSpan(14, 17, 32);
decimal hiTemp = 62.1m;
TimeSpan loTime = new TimeSpan(3, 16, 10);
decimal loTemp = 54.8m;

string result1 = String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)",
                               date1, hiTime, hiTemp, loTime, loTemp);
Console.WriteLine(result1);
Console.WriteLine();

string result2 = String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)",
                               new object[] { date1, hiTime, hiTemp, loTime, loTemp });
Console.WriteLine(result2);
// The example displays output like the following:
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
let date1 = DateTime(2009, 7, 1)
let hiTime = TimeSpan(14, 17, 32)
let hiTemp = 62.1m 
let loTime = TimeSpan(3, 16, 10)
let loTemp = 54.8m 

String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", date1, hiTime, hiTemp, loTime, loTemp)
|> printfn "%s\n"
      
String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", [| date1 :> obj; hiTime; hiTemp; loTime; loTemp |])
|> printfn "%s"
// The example displays output like the following:
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
Module Example
   Public Sub Main()
      Dim date1 As Date = #7/1/2009#
      Dim hiTime As New TimeSpan(14, 17, 32)
      Dim hiTemp As Decimal = 62.1d 
      Dim loTime As New TimeSpan(3, 16, 10)
      Dim loTemp As Decimal = 54.8d 

      Dim result1 As String = String.Format("Temperature on {0:d}:{5}{1,11}: {2} degrees (hi){5}{3,11}: {4} degrees (lo)", _
                                           date1, hiTime, hiTemp, loTime, loTemp, vbCrLf)
      Console.WriteLine(result1)
      Console.WriteLine()
           
      Dim result2 As String = String.Format("Temperature on {0:d}:{5}{1,11}: {2} degrees (hi){5}{3,11}: {4} degrees (lo)", _
                                            New Object() { date1, hiTime, hiTemp, loTime, loTemp, vbCrLf })
      Console.WriteLine(result2)                                            
   End Sub
End Module
' The example displays the following output:
'       Temperature on 7/1/2009:
'          14:17:32: 62.1 degrees (hi)
'          03:16:10: 54.8 degrees (lo)
'
'       Temperature on 7/1/2009:
'          14:17:32: 62.1 degrees (hi)
'          03:16:10: 54.8 degrees (lo)

È anche possibile passare gli oggetti da formattare come matrice anziché come elenco di argomenti.

using System;

public class CityInfo
{
   public CityInfo(String name, int population, Decimal area, int year)
   {
      this.Name = name;
      this.Population = population;
      this.Area = area;
      this.Year = year;
   }
   
   public readonly String Name; 
   public readonly int Population;
   public readonly Decimal Area;
   public readonly int Year;
}

public class FormatExample10
{
   public static void Main()
   {
      CityInfo nyc2010 = new CityInfo("New York", 8175133, 302.64m, 2010);
      ShowPopulationData(nyc2010);
      CityInfo sea2010 = new CityInfo("Seattle", 608660, 83.94m, 2010);      
      ShowPopulationData(sea2010); 
   }

   private static void ShowPopulationData(CityInfo city)
   {
      object[] args = { city.Name, city.Year, city.Population, city.Area };
      String result = String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", 
                                    args);
      Console.WriteLine(result); 
   }
}
// The example displays the following output:
//       New York in 2010: Population 8,175,133, Area 302.6 sq. feet
//       Seattle in 2010: Population 608,660, Area 83.9 sq. feet
open System

type CityInfo =
  { Name: string
    Population: int
    Area: Decimal
    Year: int }

let showPopulationData city =
    let args: obj[] = [| city.Name; city.Year; city.Population; city.Area |]
    String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", args)
    |> printfn "%s"

{ Name = "New York"; Population = 8175133; Area = 302.64m; Year = 2010 }
|> showPopulationData

 
{ Name = "Seattle"; Population = 608660; Area = 83.94m; Year = 2010 }      
|> showPopulationData 

// The example displays the following output:
//       New York in 2010: Population 8,175,133, Area 302.6 sq. feet
//       Seattle in 2010: Population 608,660, Area 83.9 sq. feet
Public Class CityInfo
   Public Sub New(name As String, population As Integer, area As Decimal, year As Integer)
      Me.Name = name
      Me.Population = population
      Me.Area = area
      Me.Year = year
   End Sub
   
   Public ReadOnly Name As String
   Public ReadOnly Population As Integer
   Public ReadOnly Area As Decimal
   Public ReadOnly Year As Integer
End Class

Module Example
   Public Sub Main()
      Dim nyc2010 As New CityInfo("New York", 8175133, 302.64d, 2010)
      ShowPopulationData(nyc2010)
      Dim sea2010 As New CityInfo("Seattle", 608660, 83.94d, 2010)      
      ShowPopulationData(sea2010) 
   End Sub
   
   Private Sub ShowPopulationData(city As CityInfo)
      Dim args() As Object = { city.Name, city.Year, city.Population, city.Area }
      Dim result = String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", args)
      Console.WriteLine(result) 
   End Sub
End Module
' The example displays the following output:
'       New York in 2010: Population 8,175,133, Area 302.6 sq. feet
'       Seattle in 2010: Population 608,660, Area 83.9 sq. feet

Vedi anche

Si applica a

Format(String, Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce uno o più elementi di formato in una stringa con la rappresentazione di stringa di un oggetto specificato.

public:
 static System::String ^ Format(System::String ^ format, System::Object ^ arg0);
public static string Format(string format, object arg0);
public static string Format(string format, object? arg0);
static member Format : string * obj -> string
Public Shared Function Format (format As String, arg0 As Object) As String

Parametri

format
String

Stringa di formato composito.

arg0
Object

Oggetto da formattare.

Valori restituiti

Copia di in cui tutti gli elementi di format formato vengono sostituiti dalla rappresentazione di stringa di arg0.

Eccezioni

format è null.

L'elemento di formato in format non è valido.

oppure

L'indice di un elemento di formato non è zero.

Commenti

Importante

Anziché chiamare il metodo String.Format o usare stringhe di formato composito, è possibile usare stringhe interpolate se il linguaggio li supporta. Una stringa interpolata è una stringa che contiene espressioni interpolate. Ogni espressione interpolata viene risolta con il valore dell'espressione e inclusa nella stringa di risultato quando viene assegnata la stringa. Per altre informazioni, vedere Interpolazione di stringhe (Riferimenti per C#) e Stringhe interpolate (Riferimenti per Visual Basic).

Questo metodo usa la funzionalità di formattazione composita per convertire il valore di un'espressione nella relativa rappresentazione di stringa e incorporare tale rappresentazione in una stringa.

Tuttavia, quando si chiama il String.Format metodo, non è necessario concentrarsi sull'overload specifico che si desidera chiamare. È invece possibile chiamare il metodo con una stringa di formato composito che include uno o più elementi di formato. Assegnare a ogni elemento di formato un indice numerico; il primo indice inizia da 0. Oltre alla stringa iniziale, la chiamata al metodo deve avere un numero di argomenti aggiuntivi pari a quello dei valori di indice. Ad esempio, una stringa i cui elementi di formato hanno indici pari a 0 e 1 devono avere 2 argomenti; uno con indici da 0 a 5 deve avere 6 argomenti. Il compilatore del linguaggio risolverà quindi la chiamata al metodo a un particolare overload del String.Format metodo .

Per una documentazione più dettagliata sull'uso del String.Format metodo , vedere Introduzione al metodo String.Format e Quale metodo chiamare?.

Esempio: Formattazione di un singolo argomento

Nell'esempio seguente viene usato il Format(String, Object) metodo per incorporare l'età di un individuo al centro di una stringa.

DateTime birthdate = new DateTime(1993, 7, 28);
DateTime[] dates = { new DateTime(1993, 8, 16),
                     new DateTime(1994, 7, 28),
                     new DateTime(2000, 10, 16),
                     new DateTime(2003, 7, 27),
                     new DateTime(2007, 5, 27) };

foreach (DateTime dateValue in dates)
{
   TimeSpan interval = dateValue - birthdate;
   // Get the approximate number of years, without accounting for leap years.
   int years = ((int) interval.TotalDays) / 365;
   // See if adding the number of years exceeds dateValue.
   string output;
   if (birthdate.AddYears(years) <= dateValue) {
      output = String.Format("You are now {0} years old.", years);
      Console.WriteLine(output);
   }
   else {
      output = String.Format("You are now {0} years old.", years - 1);
      Console.WriteLine(output);
   }
}
// The example displays the following output:
//       You are now 0 years old.
//       You are now 1 years old.
//       You are now 7 years old.
//       You are now 9 years old.
//       You are now 13 years old.
let birthdate = DateTime(1993, 7, 28)
let dates = 
    [ DateTime(1993, 8, 16) 
      DateTime(1994, 7, 28)
      DateTime(2000, 10, 16)
      DateTime(2003, 7, 27)
      DateTime(2007, 5, 27) ]

for dateValue in dates do
    let interval = dateValue - birthdate
    // Get the approximate number of years, without accounting for leap years.
    let years = (int interval.TotalDays) / 365
    // See if adding the number of years exceeds dateValue.
    if birthdate.AddYears years <= dateValue then
        String.Format("You are now {0} years old.", years)
    else
        String.Format("You are now {0} years old.", years - 1)
    |> printfn "%s"
// The example displays the following output:
//       You are now 0 years old.
//       You are now 1 years old.
//       You are now 7 years old.
//       You are now 9 years old.
//       You are now 13 years old.
Module Example
   Public Sub Main()
      Dim birthdate As Date = #7/28/1993#
      Dim dates() As Date = { #9/16/1993#, #7/28/1994#, #10/16/2000#, _
                              #7/27/2003#, #5/27/2007# }
      For Each dateValue As Date In dates
         Dim interval As TimeSpan = dateValue - birthdate
         ' Get the approximate number of years, without accounting for leap years.
         Dim years As Integer = CInt(interval.TotalDays) \ 365
         ' See if adding the number of years exceeds dateValue.
         Dim output As String
         If birthdate.AddYears(years) <= dateValue Then
            output = String.Format("You are now {0} years old.", years)
            Console.WriteLine(output)
         Else
            output = String.Format("You are now {0} years old.", years - 1)
            Console.WriteLine(output)   
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'       You are now 0 years old.
'       You are now 1 years old.
'       You are now 7 years old.
'       You are now 9 years old.
'       You are now 13 years old.

Vedi anche

Si applica a

Format(IFormatProvider, String, ReadOnlySpan<Object>)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce gli elementi di formato in una stringa con le rappresentazioni di stringa degli oggetti corrispondenti in un intervallo specificato. Un parametro fornisce informazioni di formattazione specifiche delle impostazioni cultura.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format(IFormatProvider? provider, string format, scoped ReadOnlySpan<object?> args);
static member Format : IFormatProvider * string * ReadOnlySpan<obj> -> string
Public Shared Function Format (provider As IFormatProvider, format As String, args As ReadOnlySpan(Of Object)) As String

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
String

Stringa di formato composito.

args
ReadOnlySpan<Object>

Intervallo di oggetti che contiene zero o più oggetti da formattare.

Valori restituiti

Copia di format in cui gli elementi di formato sono stati sostituiti dalla rappresentazione di stringa degli oggetti corrispondenti in args.

Si applica a

Format<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

public:
generic <typename TArg0, typename TArg1, typename TArg2>
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0, TArg1 arg1, TArg2 arg2);
public static string Format<TArg0,TArg1,TArg2>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0, TArg1 arg1, TArg2 arg2);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 * 'TArg1 * 'TArg2 -> string
Public Shared Function Format(Of TArg0, TArg1, TArg2) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0, arg1 As TArg1, arg2 As TArg2) As String

Parametri di tipo

TArg0

Tipo del primo oggetto da formattare.

TArg1

Tipo del secondo oggetto da formattare.

TArg2

Tipo del terzo oggetto da formattare.

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
CompositeFormat

Un oggetto CompositeFormat.

arg0
TArg0

Primo oggetto da formattare.

arg1
TArg1

Secondo oggetto da formattare.

arg2
TArg2

Terzo oggetto da formattare.

Valori restituiti

Stringa formattata.

Eccezioni

format è null.

L'indice di un elemento di formato è maggiore o uguale al numero di argomenti forniti.

Si applica a

Format<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

public:
generic <typename TArg0, typename TArg1>
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0, TArg1 arg1);
public static string Format<TArg0,TArg1>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0, TArg1 arg1);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 * 'TArg1 -> string
Public Shared Function Format(Of TArg0, TArg1) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0, arg1 As TArg1) As String

Parametri di tipo

TArg0

Tipo del primo oggetto da formattare.

TArg1

Tipo del secondo oggetto da formattare.

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
CompositeFormat

Un oggetto CompositeFormat.

arg0
TArg0

Primo oggetto da formattare.

arg1
TArg1

Secondo oggetto da formattare.

Valori restituiti

Stringa formattata.

Eccezioni

format è null.

L'indice di un elemento di formato è maggiore o uguale al numero di argomenti forniti.

Si applica a

Format<TArg0>(IFormatProvider, CompositeFormat, TArg0)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Sostituisce l'elemento di formato o gli elementi in un oggetto CompositeFormat con la rappresentazione di stringa degli oggetti corrispondenti nel formato specificato.

public:
generic <typename TArg0>
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0);
public static string Format<TArg0>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 -> string
Public Shared Function Format(Of TArg0) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0) As String

Parametri di tipo

TArg0

Tipo del primo oggetto da formattare.

Parametri

provider
IFormatProvider

Oggetto che fornisce informazioni di formattazione specifiche delle impostazioni cultura.

format
CompositeFormat

Un oggetto CompositeFormat.

arg0
TArg0

Primo oggetto da formattare.

Valori restituiti

Stringa formattata.

Eccezioni

format è null.

L'indice di un elemento di formato è maggiore o uguale al numero di argomenti forniti.

Si applica a