Comment Conditionnellement Format d'une Chaîne .Net?

Je voudrais faire une condition de formatage de chaînes de caractères. Je sais que vous pouvez faire un peu de mise en forme conditionnelle des nombres entiers et décimaux comme suit:

Int32 i = 0;
i.ToString("$#,##0.00;($#,##0.00);Zero");

Le code ci-dessus entraînerait dans un des trois formats si la variable est positif, négatif ou nul.

Je voudrais savoir si il existe un moyen d'utiliser des sections sur les arguments de la chaîne. Pour un béton, mais artificiel exemple, je serais à la recherche pour remplacer le "si" vérifier dans le code suivant:

string MyFormatString(List<String> items, List<String> values)
{
    string itemList = String.Join(", " items.ToArray());
    string valueList = String.Join(", " values.ToArray());

    string formatString;

    if (items.Count > 0)
    //this could easily be: 
    //if (!String.IsNullOrEmpty(itemList))
    {
        formatString = "Items: {0}; Values: {1}";
    }
    else
    {
        formatString = "Values: {1}";
    }

    return String.Format(formatString, itemList, valueList);
}
InformationsquelleAutor Adam Tegen | 2008-09-30