|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
StringBuilder.AppendFormat, méthode (IFormatProvider, String, Object[])
Espace de noms : System.Text
Assembly : mscorlib (dans mscorlib.dll)
public StringBuilder AppendFormat( IFormatProvider provider, string format, params Object[] args )
Paramètres
- provider
- Type : System.IFormatProvider
Objet qui fournit des informations de mise en forme spécifiques à la culture.
- format
- Type : System.String
Chaîne de format composite (consultez Remarques).
- args
- Type : System.Object[]
Tableau d'objets à mettre en forme.
Valeur de retour
Type : System.Text.StringBuilder| Exception | Condition |
|---|---|
| ArgumentNullException | |
| FormatException | |
| ArgumentOutOfRangeException |
Remarque |
|---|
Objet CultureInfo qui fournit des informations de format spécifiques à la culture. Objet NumberFormatInfo qui fournit des informations de mise en forme spécifiques à la culture pour les valeurs numériques dans args. Objet DateTimeFormatInfo qui fournit information de mise en forme spécifique à la culture pour les valeurs date et heure dans args. Implémentation IFormatProvider personnalisée qui fournit les informations de mise en forme pour un ou plusieurs objets dans args. En général, une telle implémentation implémente également l'interface ICustomFormatter. Le deuxième exemple de la section suivante illustre un appel de la méthode StringBuilder.AppendFormat(IFormatProvider, String, Object[]) avec une implémentation IFormatProvider personnalisée.
using System; using System.Text; using System.Globalization; class Sample { static StringBuilder sb = new StringBuilder(); public static void Main() { int var1 = 111; float var2 = 2.22F; string var3 = "abcd"; object[] var4 = {3, 4.4, 'X'}; Console.WriteLine(); Console.WriteLine("StringBuilder.AppendFormat method:"); sb.AppendFormat("1) {0}", var1); Show(sb); sb.AppendFormat("2) {0}, {1}", var1, var2); Show(sb); sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3); Show(sb); sb.AppendFormat("4) {0}, {1}, {2}", var4); Show(sb); CultureInfo ci = new CultureInfo("es-ES", true); sb.AppendFormat(ci, "5) {0}", var2); Show(sb); } public static void Show(StringBuilder sbs) { Console.WriteLine(sbs.ToString()); sb.Length = 0; } } /* This example produces the following results: StringBuilder.AppendFormat method: 1) 111 2) 111, 2.22 3) 111, 2.22, abcd 4) 3, 4.4, X 5) 2,22 */
using System; using System.Text; public class Customer { private string custName; private int custNumber; public Customer(string name, int number) { this.custName = name; this.custNumber = number; } public string Name { get { return this.custName; } } public int CustomerNumber { get { return this.custNumber; } } } public class CustomerNumberFormatter : IFormatProvider, ICustomFormatter { public object GetFormat(Type formatType) { if (formatType == typeof(ICustomFormatter)) return this; return null; } public string Format(string format, object arg, IFormatProvider provider) { if (arg is Int32) { string custNumber = ((int) arg).ToString("D10"); return custNumber.Substring(0, 4) + "-" + custNumber.Substring(4, 3) + "-" + custNumber.Substring(7, 3); } else { return null; } } } public class Example { public static void Main() { Customer customer = new Customer("A Plus Software", 903654); StringBuilder sb = new StringBuilder(); sb.AppendFormat(new CustomerNumberFormatter(), "{0}: {1}", customer.CustomerNumber, customer.Name); Console.WriteLine(sb.ToString()); } } // The example displays the following output: // 0000-903-654: A Plus Software
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.
Remarque