chaîne de remplacer l'utilisation de Linq en c#


public class Abbreviation
{
    public string ShortName { get; set; }
    public string LongName { get; set; }
}

J'ai une liste d'Abréviation objets comme ceci:


List abbreviations = new List();
abbreviations.add(new Abbreviation() {ShortName = "exp.", LongName = "expression"});
abbreviations.add(new Abbreviation() {ShortName = "para.", LongName = "paragraph"});
abbreviations.add(new Abbreviation() {ShortName = "ans.", LongName = "answer"});

string test = "this is a test exp. in a para. contains ans. for a question";

string result = test.Replace("exp.", "expression")
...

J'attends le résultat:
"c'est une expression de test dans un paragraphe contient réponse pour une question"

Actuellement, je suis en train de faire:


foreach (Abbreviation abbreviation in abbreviations)
{
    test = test.Replace(abbreviation.ShortName, abbreviation.LongName);
}
result = test;

Demandais si il ya une meilleure façon, en utilisant une combinaison de Linq et de Regex.

LINQ n'est pas une solution à chaque problème, ce que vous avez écrit est très bien.

OriginalL'auteur gangt | 2011-02-28